Q:

C program to find the greatest of three numbers

belongs to collection: C programs - Basic C Programs

0
Write C program takes three different numbers as the input and finds the largest number among them. 
This program make use of the nested if else statements.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

C program to find the greatest of three numbers - Source code
 #include<stdio.h>
 
int main()
{
    int n1,n2,n3;
    //Ask user to input any three integer numbers
    printf("Enter the three different numbers\n");
    //Store input values in variables for comparsion
    scanf("%d %d %d",&n1,&n2,&n3);
    printf("Entered numbers are number1 = %d\t number2 = %d\t number3 = %d\n",n1,n2,n3);
 
 
    if(n1>n2)
    {
        if(n1>n3)
        {
            printf("Number1 is the greatest among three");
        }
        else
        {
            printf("Number3 is greatest among three");
        }
    }
 
    else if(n2>n3)
    {
        printf("Number2 is the greatest among three");
    }
 
    else
    {
        printf("Number3 is the greatest among three");
    }
    return 0;
}

Program Output

Case 1:
Enter the three different numbers
34
57
11
Entered numbers are number1 = 34         number2 = 57    number3 = 11
Number2 is the greatest among three


Case 2:
Enter the three different numbers
678
863
1010
Entered numbers are number1 = 678        number2 = 863   number3 = 1010
Number3 is the greatest among three

Program Explanation


1. Take the three numbers and store it in the variables n1, n2 and n3 respectively.

2. Firstly check if the n1 is greater than n2. If it is, then check if it is also greater than n3? If this is also true than print the result as "Number1 is the greatest among three". Otherwise, it is obvious that n3 is largest among three so print the output as “Number3 is the greatest among three”.

3. If the n1 is not greater than n2, then check the relation between n2 and n3.

4. If n2 is greater than n3, then print the output as “Number2 is the greatest among three”.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to swap two numbers using a third variab... >>
<< C Hello World program...