C Program to find the largest of three numbers

For a better understanding of this C program example, we always recommend you to learn the basic topics of C programming listed below:

How to find the largest of three numbers?

In this C program, we need to check which number is the largest, and we can do that by checking and comparing each number with the other and taking the largest. Again compare the largest with the last number to get the final result.

How we find the largest of three numbers using the C program?

To implement this logic in the C program to find the largest of three numbers, we need to get the input numbers from the user. Then we have to import the header libraries to use the built-in functions, then after accepting the numbers from the user save them in three different variables. Use a  nested if-else condition to check for the largest number.

In the first, if condition, we check whether 'a' is greater than 'b' if that condition is true, then 'a' is compared with 'c'. If that condition also gets true print 'a' as largest, else print 'c' as largest. Else check if  'b' greater than 'c' if that condition is true print 'b' as the largest of three, else print 'c' as largest.

ALGORITHM

STEP 1: Import the header libraries into the C program to use the built-in functions.

STEP 2: Use the main() function to start the execution of the C program.

STEP 3: Define and initialize three variables using int datatype.

STEP 4: Accept the values of three numbers from the user and store them in three different variables. 

STEP 5: Use the if condition to check if 'a' is greater than 'b'.

STEP 6: Check the 'a' is greater than 'c'  and if so print the Largest Number is 'a'.

STEP 7: Else print the largest number is 'c'. 

STEP 8: Check if those conditions do not match, Check if 'b' is greater than 'c' if so print 'b' as the largest number

STEP 9: Using else print 'c' is the largest if no above conditions got a match in the C program.

C Source Code

                                          #include <stdio.h>
#include <math.h>

void main() {
  int a, b, c;
  printf("Enter the values of a,b and c\n");
  scanf("%d %d %d", & a, & b, & c);
  printf("a = %d\tb = %d\tc = %d\n", a, b, c);
  if (a > b) {
    if (a > c) {
      printf("A is the greatest among three\n");
    } else {
      printf("C is the greatest among three\n");
    }
  } else if (b > c) {
    printf("B is the greatest among three\n");
  } else {
    printf("C is the greatest among three\n");
  }
}
                                      

OUTPUT

Enter the values of a,b and c

23
32 
45
a = 23  b = 32  c = 45
C is the greatest among three

RUN2

Enter the values of a,b and c
234
678
195
a = 234 b = 678 c = 195
B is the greatest among three

RUN3

Enter the values of a,b and c
30 
20 
10
a = 30  b = 20  c = 10
A is the greatest among three