C Program to Calculate Maximum of three numbers
Program Description:
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.
Prerequisites:
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
a with
b and
c. i.e
(a>b) &&
(a>c)
- 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
false, then
a is not maximum, So we need to check the variable
b and
c
- 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
a and
b.
- 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
/* Program: Program to check the maximum of three numbers Author: Sillycodes.com */ #include<stdio.h> int main() { int a,b,c,max; printf("Enter Three numbers : "); scanf("%d%d%d",&a,&b,&c); // Check if the 'a' is greater than 'b' and 'c' if( (a>b) && (a>c) ) { // 'a' is larger than 'b', 'c' max = a; } else if(b>c) { // 'b' is maximum max = b; } else { // 'c' is maximum max = c; } // print the result printf("Max of %d, %d and %d is : %d \n", a, b, c, max); return 0; } |
Program Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
$ gcc maximum-of-three-numbers.c $ ./a.out Enter Three numbers : 100 200 300 Max of 100, 200 and 300 is : 300 $ ./a.out Enter Three numbers : -345 0 899 Max of -345, 0 and 899 is : 899 $ ./a.out Enter Three numbers : 0 0 2 Max of 0, 0 and 2 is : 2 $ ./a.out Enter Three numbers : -1 -10 -100 Max of -1, -10 and -100 is : -1 $ ./a.out Enter Three numbers : 1000 2000 3000 Max of 1000, 2000 and 3000 is : 3000 $ ./a.out Enter Three numbers : 10 10 10 Max of 10, 10 and 10 is : 10 $ |
Nice blog, keep sharing all type of program which is useful and very informative for student, if you want to more information about c language then visit my profile….