C Program to Find the Largest Number Among Three Numbers

In C programming, the largest element of a number is the number with the highest numerical value of the three numbers.

For example, if three numbers are given, 1, 2, 3, the largest of the three numbers is 3.

Problem Description

Write a C program that takes the 3 numbers and finds the largest number Aamong three numbers.

Problem Solution

Alorithm:
1. Take the three numbers as input and store them in variables.
2. Check the first number if it is greater than other two.
3. Repeat the step 2 for other two numbers.
4. Print the number which is greater among all and exit.

There are several ways to find the largest number among three numbers in C language. Let’s take a detailed look at all the approaches to find the largest/biggest of 3 numbers.

Method 1: (Using If Statement)

In this approach, we will calculate the largest of three numbers using if statement.

advertisement
advertisement

Example:
Input:
Enter three numbers:
a = 6; b = 8; c = 10

Step 1: Check if (a > b && a > c) i.e if(6 > 8 && 6 > 10). Since 6 is not greater than 8 and 10, so go to the next condition.
Step 2: Check if (b > a && b > c) i.e if(8 > 6 && 8 > 10). Here 8 is greater than 6, but 8 > 10 does not satisfy the condition, so we move on to the next condition.
Step 3: Check if (c > a && c > b) i.e if(10 > 6 && 10 > b). Since 10 is greater than 6 and 8, so the largest number is 10.

Output:
Biggest number is 10

Program/Source Code

Here is source code of the C program to calculate the largest of 3 numbers. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1. /*
  2.  * C program to find the largest  of three numbers
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     int a, b, c;
  10.     printf("Enter three numbers: \na: ");
  11.     scanf("%d", &a);
  12.     printf("b: ");
  13.     scanf("%d", &b);
  14.     printf("c: ");
  15.     scanf("%d", &c);
  16.     if (a > b && a > c)
  17.         printf("Biggest number is %d", a);
  18.     if (b > a && b > c)
  19.         printf("Biggest number is %d", b);
  20.     if (c > a && c > b)
  21.         printf("Biggest number is %d", c);
  22.     return 0;
  23. }
Program Explanation

1. Take the three numbers and store it in the variables a, b and c respectively.
2. Firstly check if the a is greater than b.
3. If it is, then check if it is greater than c.
4. If it is, then print the output as “Biggest number is a value”.
5. Check if the b is greater than a.
6. If it is, then check if it is greater than c.
7. If it is, then print the output as “Biggest number is b value”.
8. Check if the c is greater than a.
9. If it is, then check if it is greater than b.
10. If it is, then print the output as “Biggest number is c value”.

Runtime Test Cases

Testcase 1: In this case, we enter the values ​​”6, 8 and 10” as input to find the largest of the three given numbers.

Enter three numbers:
a: 6
b: 8
c: 10
Biggest number is 10

Testcase 2: In this case, we enter the values ​​”10, 99 and 87” as input to find the largest of the three given numbers.

advertisement
Enter three numbers:
a: 10
b: 99
c: 87
Biggest number is 99

Method 2: (Using If-else Statement)

In this approach, we will calculate the largest of three numbers using simple if-else statement.

Example:
Input:
Enter three numbers:
num1 = 65; num2 = 34; num3 = 25;

Check if(num1 > num2) i,e 65 > 34. Since it is true, it will check another condition, i.e. if(num1 > num3) i.e 65 > 25. 65 is greater than 25, so it will print “65 is the largest number.”

advertisement

Output:
65 is the largest number.

Program/Source Code

Here is source code of the C program to calculate the biggest of 3 numbers using if else statement. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to find the biggest of three numbers using if else statement
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. void main()
  8. {
  9.     int num1, num2, num3;
  10.  
  11.     printf("Enter the values of num1, num2 and num3\n");
  12.     scanf("%d %d %d", &num1, &num2, &num3);
  13.     printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3);
  14.     if (num1 > num2)
  15.     {
  16.         if (num1 > num3)
  17.         {
  18.             printf("%d is the largest number.", num1);
  19.         }
  20.         else
  21.         {
  22.             printf("%d is the largest number.", num3);
  23.         }
  24.     }
  25.     else if (num2 > num3)
  26.         printf("%d is the largest number.", num2);
  27.     else
  28.         printf("%d is the largest number.", num3);
  29. }
Program Explanation

1. Take the three numbers and store it in the variables num1, num2 and num3 respectively.
2. Firstly check if the num1 is greater than num2.
3. If it is, then check if it is greater than num3.
4. If it is, then print the output as “num1 value is the largest number.“.
5. Otherwise print the ouput as “num3 value is the largest number.“.
6. If the num1 is not greater than num2, then check if num2 is greater than num3.
7. If it is, then print the output as “num2 value is the largest number.“.
8. Otherwise print the output as “num3 value is the largest number.“.

Runtime Test Cases

Testcase 1: In this case, we enter the values ​​”65“, “34” and “25” as input to find the largest of the three given numbers.

Enter the values of num1, num2 and num3
65
34
25
num1 = 65  num2 = 34  num3 = 25
65 is the largest number.

Testcase 2: In this case, we enter the values ​​”8“, “4“, and “19” as input to find the largest of the three given numbers.

Enter the values of num1, num2 and num3
8
4
19
num1 = 8  num2 = 4  num3 = 19
19 is the largest number.

Method 3: (Using Ternary Operator)

In this approach, we will use ternary operator to find the biggest number.

Ternary Operator:

The general form of ternary operator is : (condition) ? (if true) : (if false)

Example:
Input:
Enter three numbers:
a = 22; b = 14; c = 51

Ternary condition:

a > b ? (a > c ? a : c) : (b > c ? b : c));

Here, a > b -> Condition. If it satisfies it will check (a > c ? a : c) else (b > c ? b : c).

22 > 14, therefore it will look for (a > c ? a : c). i.e 22 > 51. 22 is not greater than 51. It will print the biggest number, which is 51.

Output:
Largest of three numbers is 51

Program/Source Code

Here is source code of the C program to calculate the biggest of 3 numbers using ternary operator. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to find the biggest of three numbers using ternary operator
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int main(void)
  8. {
  9.     int a, b, c;
  10.     printf("Enter three numbers: \na: ");
  11.     scanf("%d", &a);
  12.     printf("b: ");
  13.     scanf("%d", &b);
  14.     printf("c: ");
  15.     scanf("%d", &c);
  16.     printf("Largest of three numbers is %d", a > b ? (a > c ? a : c) : (b > c ? b : c));
  17.     return 0;
  18. }
Program Explanation

1. Take the three numbers and store it in the variables a, b and c respectively.
2. The general form of ternary operator is : (condition) ? (if true) : (if false)
i.e. a > b ? (a > c ? a : c) : (b > c ? b : c));
3. In the above program, it signifies that if a is greater than b, then return a if a is greater than c, otherwise return c.
4. If it is a is less than b, then return b if b is greater than c, otherwise return c.

Runtime Test Cases

Testcase 1: In this case, we enter the values ​​”1“, “9” and “37” as input to find the largest of the three given numbers.

Enter three numbers: 
a: 1
b: 9
c: 37
Largest of three numbers is 37

Testcase 2: In this case, we enter the values ​​”22“, “14“, and “51” as input to find the largest of the three given numbers.

Enter three numbers: 
a: 22
b: 14
c: 51
Largest of three numbers is 51

Method 4: (Using Function)

In this approach, we will calculate the biggest of three numbers using functions.

Functions used:
biggest(int, int): This function will find the biggest of two numbers.

Program/Source Code

Here is source code of the C program to calculate the biggest of 3 numbers using function. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to find the biggest of three numbers using function
  3.  */
  4.  
  5. #include <stdio.h>
  6.  
  7. int biggest(int a, int b)
  8. {
  9.     if (a > b)
  10.         return a;
  11.     return b;
  12. }
  13.  
  14. int main(void)
  15. {
  16.     int a, b, c;
  17.     printf("Enter three numbers: \na: ");
  18.     scanf("%d", &a);
  19.     printf("b: ");
  20.     scanf("%d", &b);
  21.     printf("c: ");
  22.     scanf("%d", &c);
  23.     printf("%d is the biggest of all three numbers.\n", biggest(biggest(a, b), c));
  24.     return 0;
  25. }
Program Explanation

1. Take the three numbers and store it in the variables a, b and c respectively.
2. The program will check if a is greater than b. If it is, it will check if a is greater than c. If it is, it will print a. Otherwise, it will print c.

Runtime Test Cases

Testcase 1: In this case, we enter the values ​​”99“, “132” and “88” as input to find the largest of the three given numbers.

Enter three numbers: 
a: 99
b: 132
c: 88
132 is the biggest of all three numbers.

Testcase 2: In this case, we enter the values ​​”29320“, “41332“, and “42393” as input to find the largest of the three given numbers.

Enter three numbers: 
a: 29320
b: 41332
c: 42393
42393 is the biggest of all three numbers.

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.