C Program to Calculate Maximum of three numbers
Program to calculate the maximum of three numbers in C programming language.
The program will accept the three numbers from the user and then calculate the maximum of them. and display the result on the console.
We are going to use the decision-making statement i.e if else statement with the Relations operators( >, <, etc) to calculate the maximum.
It is recommended to have a basic knowledge of if-else statements and Relational operators. please go through the following articles
- Relational Operators in C
- Decision making statements if and if…else in C
- Nested if else and if..else ladder in C-Language
Maximum of three numbers Program Explanation:
- We start the program by asking the user for input and once we got the three numbers and we store them in variables a, b, and c.
- Then we will check if the
a is maximum by comparing
- We have AND&& operation, means both a > b and a > c need to be true
- If the above condition is true then the a is maximum number. Save the a value in max variable.
- If the above condition is
a is not maximum, So we need to check the variable
- check if the variable b is maximum by comparing the b and c.
- if b is larger than c ( i.e b > c). Then b is the maximum of three number. save the value of b in max variable
- If above two conditions are
false, Then the variable
c is bigger than both variables
- Store the value of the c in the max variable
- Finally, We printed the maximum number on to the console using the printf function.
Maximum of three numbers program in C:
Program: Program to check the maximum of three numbers
printf("Enter Three numbers : ");
// Check if the 'a' is greater than 'b' and 'c'
if( (a>b) && (a>c) )
// 'a' is larger than 'b', 'c'
max = a;
// 'b' is maximum
max = b;
// 'c' is maximum
max = c;
// print the result
printf("Max of %d, %d and %d is : %d \n", a, b, c, max);
$ gcc maximum-of-three-numbers.c $ ./a.out
Enter Three numbers : 100 200 300
Max of 100, 200 and 300 is : 300
Enter Three numbers : -345 0 899
Max of -345, 0 and 899 is : 899
Enter Three numbers : 0 0 2
Max of 0, 0 and 2 is : 2
Enter Three numbers : -1 -10 -100
Max of -1, -10 and -100 is : -1
Enter Three numbers : 1000 2000 3000
Max of 1000, 2000 and 3000 is : 3000
Enter Three numbers : 10 10 10
Max of 10, 10 and 10 is : 10