In this program, we are going to discuss how to find the greatest number from the four numbers. So let’s start!
C Program to Find Greatest of Four Numbers
Basically, there are two ways to find the maximum number we see one by one. In the first example, we use user define function so let’s see how we find the maximum from four numbers in the second example we use the condition statement.
For example –
Input :
We are taking user input so first we are entering four numbers from the user, for example, we are taking
11 33 55 88
Logic:
int max(int x,int y){
if(x>y){
return x;
}
else
{
return y;
}
Output:
From the above input, our output is
The maximum number is 88
Method 1:
// C program to find the maximum from the four numbers by using a user-defined function.
#include<stdio.h>
int max(int x,int y){
if(x>y){
return x;
}
else
{
return y;
}
}
int main(){
int a,b,c,d;
printf(“Enter a four number :”);
scanf(“%d%d%d%d”,&a,&b,&c,&d);
int first_max = max(a,b);
int second_max = max(c,d);
int final_max = max(first_max, second_max);
printf(“Maximum number is %d”, final_max);
return 0;
}
Output :
Enter a four number :
11 33 55 88
The maximum number is 88
Explanation :
In the above program, we create a user define a function in which we use two integer values.
- Then we apply the if condition to check which number is the biggest.
- Then we make the main function in which we get four numbers from a user that’s why we use scanf.
- Other than these we make 3 variables.
- first_max to store a maximum number between a and b.
- second_max to store a maximum number between c and d.
- after that, we use final_max to find the maximum between first_max and second_max and after this, we get our final and mean final maximum number.
Conclusion :
In this way, we calculate the maximum number from the four numbers. I hope you guys understand this program.
Method 2:
//C program to find the maximum from the four numbers by using the condition statement :
#include<stdio.h>
int main(){
int a,b,c,d, big1, big2;
printf(“Enter four numbers:”);
scanf(“%d%d%d%d”, &a,&b,&c,&d);
if(a>b)
{
big1 = a;
}
else{
big1 =b;
}
if(c>d){
big2 =c;
}
else{
big2 =d;
}
if(big1>big2){
printf(“%d is big”, big1);
}
else{
printf(“%d is big”,big2);
}
return 0;
}
Output
Enter four numbers: 11 44 22 88
88 is big.
First of all, we have four numbers and we have to find out the biggest number from them. So for that, we compare the first two numbers such as a and b, and then c and d.
From the above example, we can simply compare the first two numbers and then find out the maximum from that two numbers that is a and b and store the maximum number in big1.
The same procedure is followed for c and d also and then, stores the maximum number in big2.
And then compare that big1 and big2 and we will get our final answer.
In this way, we simply find out the maximum number from four numbers by using these methods. You can also explore these examples by taking other variables or putting another number in them. I hope this concept is clear to you. So for now keep exploring and keep learning.