C program to find maximum number among three numbers
C program to find maximum number among three numbers
C program to input any three numbers from user and find maximum among three numbrs using if...else and if...else...if. Let's see the below step by step logic to find the maximum number among three numbers.
Before writing any program you just imagine about output screen first. Like what input to be given, and output to be displayed. Let's have a look at the below example.
- INPUT
- Enter any three numbers: 15, 30, 60
OUTPUT
60 is maximum.
Required knowledge for this exercise
Logic to find maximum between two numbers:
In C programming, finding maximum or minimum between two or more numbers we need comparision of all numbers unig relational operator either >
or <
along with if...else
to find maximum or minimum number. Relational operator evaluates either 1 (true
) or 0 (false
) depending on condition.
1) Input three numbers from user and store it in variables num1
,num2
and num3
using scanf()
2) Compare num1
and num2
i.eif(num1 > num2)
If it's true then perform one more comparision between num1
andnum3
i.e if(num1 > num3) if it's also true then num1
is greater otherwise num3
3)if(num1 > num2)
If it's flase then it means is less than . Hence, this time compare between num2
andnum3
i.e if(num2 > num3)
If it's true then num2 is maximum otherwise num3.
4) finally print the value of max. that's it!
Program to find maximum number among three umbers using nested if...else:
-
#include <stdio.h>
int main()
{
int num1, num2, num3, max;
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);
if(num1 > num2)
{
if(num1 > num3)
{
max = num1;
}
else
{
max = num3;
}
}
else
{
if(num2 > num3)
{
max = num2;
}
else
{
max = num3;
}
}
printf("Maximum among all three numbers = %d", max);
return 0;
}
The above approach is lengthy. In place of nested if you can use ladder if...else...if with relational and logical operator to make it more simple way.
Program to find maximum number among three numbers using ladder if...else...if:
Here we are using logical AND &&
operator to combine two conditions togather. To find maximum number among three numbers, we need to compare each number with rest of two numbers. Let's see the below logic to check maximum among three numbers.
if((num1 > num2) && (num1 > num3)) is true then num1 is maximum
if((num1 > num2) && (num1 > num3)) is true then num2 is maximum
if((num1 > num2) && (num1 > num3)) is true then num3 is maximum
-
#include <stdio.h>
int main()
{
int num1, num2, num3, max;
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);
if((num1 > num2) && (num1 > num3))
{
max = num1;
}
else if((num2 > num1) && (num2 > num3))
{
max = num2;
}
else if((num3 > num1) && (num3 > num2))
{
max = num3;
}
printf("Maximum among all three numbers = %d", max);
return 0;
}
Above program can also be written as below with a little bit chages:
Instead of checking six unnecessary conditions you can further short the logic using below approach.
-
#include <stdio.h>
int main()
{
int num1, num2, num3, max;
printf("Enter three numbers: ");
scanf("%d%d%d", &num1, &num2, &num3);
if((num1 > num2) && (num1 > num3))
{
max = num1;
}
else if(num2 > num3)
{
max = num2;
}
else
{
max = num3;
}
printf("Maximum among all three numbers = %d", max);
return 0;
}
Output:
- Enter three number: 15, 30, 60
- Maximum among all three numbers = 60
Help others by sharing this page.
Ahmad Irshad
Author & Editor
I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.