C Program to Find the Greatest of the Three Numbers

Find the Greatest of Three Numbers in C

Given three integers num1, num2, and num3 as inputs.

The objective is to write a code to Find the Greatest of the Three Numbers in C Language. To do so we’ll check the numbers with each other and print the largest of them all.

Example

Input : num1 = 2 num2 = 9 num3 = 4

Output : 9

Flow Diagram:-

Greatest of the Three numbers.jpg

Code:-

  • Declare a variable a, b, c and largest as integer;

  • Read the number a, b and c;

  • max = a > b ? (a > c ? a : c) : (b > c ? b : c);

  • print max;

Algorithm:-

Code:-

#include <stdio.h>
int main ()
{
    int num1, num2, num3;
    
    printf("Insert three numbers: ");
    scanf("%d %d %d", &num1, &num2, &num3);

    //checking if num1 is greatest
    if (num1 >= num2 && num1 >= num3)
        printf("%d is the greatest", num1);
        
    //checking if num2 is greatest
    else if(num2 >= num1 && num2 >= num3)
         printf("%d is the greatest", num2);

    //checking if num2 is greatest
    else if(num3 >= num1 && num3 >= num2)
         printf("%d is the greatest", num3);
    
    return 0;
}

Output:-

Enter a number: 10 15 20

20 is the greatest