Sunday, July 29, 2018

C program to find the largest number among the three numbers


In this example, the largest number among three numbers (entered by the user) is found using three different methods.
Largest number among three numbers using if, if...else and nested if...else statement. 

//Example1

This program uses only if statement to find the largest number.

#include <stdio.h>
int main()
{
    double n1, n2, n3;

    printf("Enter three different numbers: ");
    scanf("%lf %lf %lf", &n1, &n2, &n3);

    if( n1>=n2 && n1>=n3 )
        printf("%.2f is the largest number.", n1);

    if( n2>=n1 && n2>=n3 )
        printf("%.2f is the largest number.", n2);

    if( n3>=n1 && n3>=n2 )
        printf("%.2f is the largest number.", n3);

    return 0;
}


Output:





//Example2
 
This program uses if...else statement to find the largest number.


#include <stdio.h>
int main()
{
    double n1, n2, n3;

    printf("Enter three numbers: ");
    scanf("%lf %lf %lf", &n1, &n2, &n3);

    if (n1>=n2)
    {
        if(n1>=n3)
            printf("%.2lf is the largest number.", n1);
        else
            printf("%.2lf is the largest number.", n3);
    }
    else
    {
        if(n2>=n3)
            printf("%.2lf is the largest number.", n2);
        else
            printf("%.2lf is the largest number.",n3);
    }

    return 0;
}



Output:



//Example3

Though, the largest number among three numbers is found using multiple ways, the output of all these program will be same.

#include <stdio.h>

int main()
{
    double n1, n2, n3;

    printf("Enter three numbers: ");
    scanf("%lf %lf %lf", &n1, &n2, &n3);

    if( n1>=n2 && n1>=n3)
        printf("%.2lf is the largest number.", n1);

    else if (n2>=n1 && n2>=n3)
        printf("%.2lf is the largest number.", n2);

    else
        printf("%.2lf is the largest number.", n3);

    return 0;
}


Output:

No comments:

Post a Comment

C program for find square value

This is an example of how to find square value of a number using function.  #include<stdio.h> float square ( float x ); int ma...