w3resource logo


C Programming Exercises

C Exercises: Find the largest of three numbers

Secondary Nav

C Conditional Statement: Exercise-8 with Solution

Write a C program to find the largest of three numbers.

Test Data :
12 25 52 

Expected Output :
1st Number = 12,        2nd Number = 25,        3rd Number = 52
The 3rd Number is the greatest among three

Sample Solution :-

C Code:

#include <stdio.h>

void main()
{
    int num1, num2, num3;
 
    printf("Input the values of three numbers : ");
    scanf("%d %d %d", &num1, &num2, &num3);
    printf("1st Number = %d,\t2nd Number = %d,\t3rd Number = %d\n", num1, num2, num3);
    if (num1 > num2)
    {
        if (num1 > num3)
        {
            printf("The 1st Number is the greatest among three. \n");
        }
        else
        {
            printf("The 3rd Number is the greatest among three. \n");
        }
    }
    else if (num2 > num3)
        printf("The 2nd Number is the greatest among three \n");
    else
        printf("The 3rd Number is the greatest among three \n");
}

OR

#include <stdio.h>

void main() {
    int num1, num2, num3;
 
    printf("Input the values of three numbers : ");
    scanf("%d %d %d", &num1, &num2, &num3);
    printf("1st Number = %d,\t2nd Number = %d,\t3rd Number = %d\n", num1, num2, num3);
 
   if ((num1 > num2) && (num1 > num3))
            printf("The 1st Number is the greatest among three. \n");
 
   if ((num2 > num3) && (num2 > num1))
        printf("The 2nd Number is the greatest among three \n");
 
   if ((num3 > num1) && (num3 > num2))
           printf("The 3rd Number is the greatest among three. \n");
 
}

OR

#include <stdio.h>

void main() {
    int num1, num2, num3,ctr;
 
    printf("Input the values of three numbers : ");
    scanf("%d %d %d", &num1, &num2, &num3);
    printf("1st Number = %d,\t2nd Number = %d,\t3rd Number = %d\n", num1, num2, num3);
 
   if (num1 > num2 && num1 > num3) ctr=num1;
 
   if (num2 > num3 && num2 > num1) ctr=num2;

   if (num3 > num1 && num3 > num2) ctr=num3;
           printf("The %d is the greatest among three. \n",ctr);
 
}

Flowchart :

Flowchart: Find the largest of three numbers.

 

Code Editor :

#include <stdio.h>

void main()
{
    int num1, num2, num3;
 
    printf("Input the values of three numbers : ");
    scanf("%d %d %d", &num1, &num2, &num3);
    printf("1st Number = %d,\t2nd Number = %d,\t3rd Number = %d\n", num1, num2, num3);
    if (num1 > num2)
    {
        if (num1 > num3)
        {
            printf("The 1st Number is the greatest among three. \n");
        }
        else
        {
            printf("The 3rd Number is the greatest among three. \n");
        }
    }
    else if (num2 > num3)
        printf("The 2nd Number is the greatest among three \n");
    else
        printf("The 3rd Number is the greatest among three \n");
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.