C program to find greatest of three numbers
In this post, you will learn how to find the greatest of three numbers using the C programming language.
Such a type of logical programming can be asked in a programming interview. It helps to improve the thinking power of a new developer. There are different ways to find the largest of the three numbers in C. Here, we have mentioned most of them.
Find the greatest of 3 numbers using If Statement
Here, we have only used the if statement to find the greatest of three numbers in C. This program prompts the user to enter three number inputs. The program compares all three numbers and returns the greatest number in the output.
#include <stdio.h>
int main() {
int n1, n2, n3;
// Getting three integer values from user
printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%d is the largest number.", n1);
if (n2 >= n1 && n2 >= n3)
printf("%d is the largest number.", n2);
if (n3 >= n1 && n3 >= n2)
printf("%d is the largest number.", n3);
return 0;
}
Output 1
Enter three different numbers: 45
93
15
93 is the largest number.
Output 2
Enter three different numbers: 62
52
15
62 is the largest number.
Find the greatest of 3 numbers using If Else Statement
Here, we have asked the user to enter three numbers and used the if and else if statements to find the greatest number in C.
#include <stdio.h>
int main() {
int n1, n2, n3;
// Getting three integer values from user
printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%d is the largest number.", n1);
else if (n2 >= n1 && n2 >= n3)
printf("%d is the largest number.", n2);
else
printf("%d is the largest number.", n3);
return 0;
}
Output 1
Enter three different numbers: 733
124
642
733 is the largest number.
Find the greatest of 3 numbers using nested if-else statements
Here, we have asked the user to enter three numbers and used the nested if-else statements to find the greatest number in C.
#include <stdio.h>
int main() {
int n1, n2, n3;
// Getting three integer values from user
printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if (n1 >= n2) {
if (n1 >= n3)
printf("%d is the largest number.", n1);
else
printf("%d is the largest number.", n3);
}
else {
if (n2 >= n3)
printf("%d is the largest number.", n2);
else
printf("%d is the largest number.", n3);
}
return 0;
}
Output of the above code -
Enter three different numbers: 123
532
241
532 is the largest number.
Find the greatest of 3 numbers using ternary operator
Here, we have used the ternary operator to find the greatest number in C.
#include <stdio.h>
int main()
{
int n1, n2, n3, largest;
printf("Enter three numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
largest = n1 > n2 ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3);
printf("%d is the largest number.", largest);
return 0;
}
Output of the above code:
Enter three numbers: 67 34 90
90 is the largest number.
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative