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.
Write a C program that takes the 3 numbers and finds the largest number Aamong three numbers.
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.
- Largest of Three Numbers in C using If Statement
- Largest of Three Numbers in C using If-else Statement
- Largest of Three Numbers in C using Ternary Operator
- Largest of Three Numbers in C using Function
In this approach, we will calculate the largest of three numbers using if statement.
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
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.
/*
* C program to find the largest of three numbers
*/
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers: \na: ");
scanf("%d", &a);
printf("b: ");
scanf("%d", &b);
printf("c: ");
scanf("%d", &c);
if (a > b && a > c)
printf("Biggest number is %d", a);
if (b > a && b > c)
printf("Biggest number is %d", b);
if (c > a && c > b)
printf("Biggest number is %d", c);
return 0;
}
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”.
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.
Enter three numbers: a: 10 b: 99 c: 87 Biggest number is 99
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.”
Output:
65 is the largest number.
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.
/*
* C program to find the biggest of three numbers using if else statement
*/
#include <stdio.h>
void main()
{
int num1, num2, num3;
printf("Enter the values of num1, num2 and num3\n");
scanf("%d %d %d", &num1, &num2, &num3);
printf("num1 = %d\tnum2 = %d\tnum3 = %d\n", num1, num2, num3);
if (num1 > num2)
{
if (num1 > num3)
{
printf("%d is the largest number.", num1);
}
else
{
printf("%d is the largest number.", num3);
}
}
else if (num2 > num3)
printf("%d is the largest number.", num2);
else
printf("%d is the largest number.", num3);
}
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.“.
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.
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
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.
/*
* C program to find the biggest of three numbers using ternary operator
*/
#include <stdio.h>
int main(void)
{
int a, b, c;
printf("Enter three numbers: \na: ");
scanf("%d", &a);
printf("b: ");
scanf("%d", &b);
printf("c: ");
scanf("%d", &c);
printf("Largest of three numbers is %d", a > b ? (a > c ? a : c) : (b > c ? b : c));
return 0;
}
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.
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
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.
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.
/*
* C program to find the biggest of three numbers using function
*/
#include <stdio.h>
int biggest(int a, int b)
{
if (a > b)
return a;
return b;
}
int main(void)
{
int a, b, c;
printf("Enter three numbers: \na: ");
scanf("%d", &a);
printf("b: ");
scanf("%d", &b);
printf("c: ");
scanf("%d", &c);
printf("%d is the biggest of all three numbers.\n", biggest(biggest(a, b), c));
return 0;
}
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.
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”.
- Apply for C Internship
- Check Computer Science Books
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Check C Books