Greatest of Three Numbers in C Program using If | If-else | Switch Case | Ternary Operator | Function | Pointers
Greatest of three numbers in c program using if, if-else, switch case, ternary operator, function, and pointers.
In this article, you will learn how to find the largest number from among three input numbers. We'll use various approaches to solve this problem like (if, if-else, if...else ladder, ternary operator, switch case, function, and pointers)
, etc.
Example
INPUT-1: 4
INPUT-2: 7
INPUT-3: 9
Greatest Number :: 9
You should know about the following topics in C programming to understand this program:
- C If statement
- C If-else statement
- C Nested If-else statement
- C Ternary operator
- C Switch Case
- C Function
- C Pointers
1 - Greatest of Three Numbers in C using If Statement
// Greatest of Three Numbers in C using If Statement
#include <stdio.h>
// It's the main function of the program
int main()
{
int num1, num2, num3;
// Step-1 Enter three integer values
printf("INPUT-1: ");
scanf("%d", &num1);
printf("INPUT-2: ");
scanf("%d", &num2);
printf("INPUT-3: ");
scanf("%d", &num3);
// Step-2 Check if num1 is greater than both num2 and num3, num1 is the greatest
if (num1 >= num2 && num1 >= num3)
{
printf("\nGreatest Number :: %d", num1);
}
// Step-3 Check if num2 is greater than both num1 and num3, num2 is the greatest
if (num2 >= num1 && num2 >= num3)
{
printf("\nGreatest Number :: %d", num2);
}
// Step-4 Check if num3 is greater than both num1 and num2, num3 is the greatest
if (num3 >= num1 && num3 >= num2)
{
printf("\nGreatest Number :: %d", num3);
}
return 0;
}
Output
INPUT-1: 4
INPUT-2: 7
INPUT-3: 9
Greatest Number :: 9
2 - Greatest of Three Numbers in C using If...else Ladder
// Greatest of Three Numbers in C using If...else Ladder
#include <stdio.h>
// It's the main function of the program
int main()
{
int num1, num2, num3;
// Step-1 Enter three integer values
printf("INPUT-1: ");
scanf("%d", &num1);
printf("INPUT-2: ");
scanf("%d", &num2);
printf("INPUT-3: ");
scanf("%d", &num3);
// Step-2 Check if num1 is greater than both num2 and num3, num1 is the greatest
if (num1 >= num2 && num1 >= num3)
{
printf("\nGreatest Number :: %d", num1);
}
// Step-3 Check if num2 is greater than both num1 and num3, num2 is the greatest
else if (num2 >= num1 && num2 >= num3)
{
printf("\nGreatest Number :: %d", num2);
}
// Step-4 If above both conditions are false then, num3 is the greatest
else
{
printf("\nGreatest Number :: %d", num3);
}
return 0;
}
3 - Greatest of Three Numbers in C using Nested If-else
// Greatest of Three Numbers in C using Nested If-else
#include <stdio.h>
// It's the main function of the program
int main()
{
int num1, num2, num3;
// Step-1 Enter three integer values
printf("INPUT-1: ");
scanf("%d", &num1);
printf("INPUT-2: ");
scanf("%d", &num2);
printf("INPUT-3: ");
scanf("%d", &num3);
// Step-2 Check if num1 is greater than both num2 and num3, num1 is the greatest
if (num1 >= num2 && num1 >= num3)
{
printf("\nGreatest Number :: %d", num1);
}
else
{
// Step-3 Check if num2 is greater than both num1 and num3, num2 is the greatest
if (num2 >= num1 && num2 >= num3)
{
printf("\nGreatest Number :: %d", num2);
}
// Step-4 If above both conditions are false then, num3 is the greatest
else
{
printf("\nGreatest Number :: %d", num3);
}
}
return 0;
}
Output
INPUT-1: 10
INPUT-2: 8
INPUT-3: 17
Greatest Number :: 17
4 - Greatest of Three Numbers in C using Ternary Operator
// Greatest of Three Numbers in C using Ternary Operator
#include <stdio.h>
// It's the main function of the program
int main()
{
int num1, num2, num3;
// Step-1 Enter three integer values
printf("INPUT-1: ");
scanf("%d", &num1);
printf("INPUT-2: ");
scanf("%d", &num2);
printf("INPUT-3: ");
scanf("%d", &num3);
// Step-2 Compare each value to find the greatest
// If num1 is greater than both num2 and num3, num1 is the greatest
num1 >= num2 &&num1 >= num3
? printf("\nGreatest Number :: %d", num1)
: (
// If num2 is greater than both num1 and num3, num2 is the greatest
num2 >= num1 && num2 >= num3
? printf("\nGreatest Number :: %d", num2)
// If the above conditions are false then, num3 is the greatest
: printf("\nGreatest Number :: %d", num3));
return 0;
}
Output
INPUT-1: 90
INPUT-2: 25
INPUT-3: 110
Greatest Number :: 110
5 - Greatest of Three Numbers in C using Switch Case
// Greatest of Three Numbers in C using Switch Case
#include <stdio.h>
// It's the main function of the program
int main()
{
int num1, num2, num3;
// Step-1 Enter three integer values
printf("INPUT-1: ");
scanf("%d", &num1);
printf("INPUT-2: ");
scanf("%d", &num2);
printf("INPUT-3: ");
scanf("%d", &num3);
int largest = num1;
// Step-2 Compare num2 & num3 to find the largest
switch ((num1 > num2) * 1 + (num1 > num3) * 2 + (num2 > num3) * 4)
{
case 0:
largest = num2 > num3 ? num2 : num3; // Choose the larger of num2 or num3
break;
case 1: // num1 is greater than num2 and num3
largest = num1;
break;
case 2: // num1 is greater than num3 but less than num2
largest = num2;
break;
case 3: // num1 is greater than num2, num2 is greater than num3
largest = num1;
break;
case 4: // num1 is less than num2, num1 is greater than num3
largest = num3;
break;
case 5: // num1 is greater than num3, num2 is greater than num3
largest = num2;
break;
case 6: // num2 is greater than num1, num2 is greater than num3
largest = num2;
break;
case 7: // num2 is greater than num1 and num3
largest = num2;
break;
}
// Step-3 Final output of the program
printf("\nGreatest Number :: %d", largest);
return 0;
}
Output
INPUT-1: 4
INPUT-2: 5
INPUT-3: 3
Greatest Number :: 5
6 - Greatest of Three Numbers in C using Function
// Greatest of Three Numbers in C using Function
#include <stdio.h>
// Function to find the greatest number
int biggest_number(int num1, int num2, int num3)
{
if (num1 >= num2 && num1 >= num3)
return num1;
else if (num2 >= num1 && num2 >= num3)
return num2;
else
return num3;
}
// It's the main function of the program
int main()
{
int num1, num2, num3;
// Step-1 Enter three integer values
printf("INPUT-1: ");
scanf("%d", &num1);
printf("INPUT-2: ");
scanf("%d", &num2);
printf("INPUT-3: ");
scanf("%d", &num3);
// Step-2 Call the biggest_number() function and find the greatest number
printf("\nGreatest Number :: %d", biggest_number(num1, num2, num3));
return 0;
}
7 - Greatest of Three Numbers in C using Pointers
// Greatest of Three Numbers in C using Pointers
#include <stdio.h>
// It's the main function of the program
int main()
{
int num1, num2, num3;
int *p_num1, *p_num2, *p_num3;
// Step-1 Enter three integer values
printf("INPUT-1: ");
scanf("%d", &num1);
printf("INPUT-2: ");
scanf("%d", &num2);
printf("INPUT-3: ");
scanf("%d", &num3);
// Step-2 Reference the values to pointer variables
p_num1 = &num1;
p_num2 = &num2;
p_num3 = &num3;
// Step-2 Check if p_num1 is greater than both p_num2 and p_num3, p_num1 is the greatest
if (*p_num1 >= *p_num2 && *p_num1 >= *p_num3)
{
printf("\nGreatest Number :: %d", *p_num1);
}
// Step-3 Check if p_num2 is greater than both p_num1 and p_num3, p_num2 is the greatest
else if (*p_num2 >= *p_num1 && *p_num2 >= *p_num3)
{
printf("\nGreatest Number :: %d", *p_num2);
}
// Step-4 If above both conditions are false then, p_num3 is the greatest
else
{
printf("\nGreatest Number :: %d", *p_num3);
}
return 0;
}
Output
INPUT-1: 90
INPUT-2: 40
INPUT-3: 10
Greatest Number :: 90