Find the Greatest of Two Numbers in C Program
ADVERTISEMENTS
Find the greatest of two numbers in c program.
In this article, you will learn how to compare the two input integer values with each other to find the greatest number. To solve this problem we'll use the various approaches like If-else statement
, Nested if-else statement
, and Ternary opertaor
.
Example-1
Enter First Number: 5
Enter Second Number: 8
Greatest Number :: 8
Example-2
Enter First Number: 6
Enter Second Number: 6
Both are equal number
You should know about the following topics in C programming to understand this program:
- C If-else statement
- C Nested if..if-else statement
- C Ternary operator
1 - Greatest of Two Numbers in C using If-else
// Greatest of Two Numbers in C using If-else
#include <stdio.h>
// It's the main function of the program
int main()
{
int number_1, number_2;
// Step-1 Take two input values from the User
printf("Enter First Number: ");
scanf("%d", &number_1);
printf("Enter Second Number: ");
scanf("%d", &number_2);
// Step-2 Check if the first number is Greatest
if (number_1 > number_2)
{
printf("\nGreatest Number :: %d\n", number_1);
}
// Step-3 Check if the second number is Greatest
else if (number_1 < number_2)
{
printf("\nGreatest Number :: %d\n", number_2);
}
// Step-4 If the previous condition is false then both numbers are equal
else
{
printf("\nBoth are equal number\n");
}
return 0;
}
Output
Enter First Number: 5
Enter Second Number: 8
Greatest Number :: 8
2 - Greatest of Two Numbers in C using Nested If-else
// Greatest of Two Numbers in C using Nested If-else
#include <stdio.h>
// It's the main function of the program
int main()
{
int number_1, number_2;
// Step-1 Take two input values from the User
printf("Enter First Number: ");
scanf("%d", &number_1);
printf("Enter Second Number: ");
scanf("%d", &number_2);
// Step-2 Check if the first number is Greatest
if (number_1 > number_2)
{
printf("\nGreatest Number :: %d\n", number_1);
}
else
{
// Step-3 Check if the second number is Greatest
if (number_1 < number_2)
{
printf("\nGreatest Number :: %d\n", number_2);
}
// Step-4 If the previous condition is false then both numbers are equal
else
{
printf("\nBoth are equal number\n");
}
}
return 0;
}
Output
Enter First Number: 10
Enter Second Number: 7
Greatest Number :: 10
3 - Greatest of Two Numbers in C using Ternary Operator
// Greatest of Two Numbers in C using Ternary Operator
#include <stdio.h>
// It's the main function of the program
int main()
{
int number_1, number_2;
// Step-1 Take two input values from the User
printf("Enter First Number: ");
scanf("%d", &number_1);
printf("Enter Second Number: ");
scanf("%d", &number_2);
// Step-2 Check if the first number is the Greatest or second number is the Greatest or both numbers are equal
number_1 > number_2
? printf("\nGreatest Number :: %d\n", number_1)
: (
number_1 < number_2
? printf("\nGreatest Number :: %d\n", number_2)
: printf("\nBoth are equal number\n")
);
return 0;
}
Output
Enter First Number: 9
Enter Second Number: 9
Both are equal number