C program to find the largest or greatest of three numbers; Through this tutorial, we will learn how to find the largest of three numbers in the c program using if statement, ternary operator and nested if else statement.
Algorithm and Programs to find Largest of Three Numbers
Use the following algorithm and program to find largest of 3/threee numbers in c:
- Aglorithm to Find Largest of Three Numbers
- C Program to Find Largest of Two Numbers using Else If Statement
- C Program to Find Largest of Two Numbers using Ternary Operator
- C Program to Find Largest of Two Numbers using Nested If Else Statement
Aglorithm to Find Largest of Three Numbers
Use the following algorithm to write a c program to find largest of three number; as follows:
- Start program
- Read the three integer values in program.
- Check if num1 is greater than num2.
- If true, then check if num1 is greater than num3.If true, then print ‘num1’ as the greatest number.
- If false, then print ‘num3’ as the greatest number.
- If false, then check if num2 is greater than num3.If true, then print ‘num2’ as the greatest number.
- If false, then print ‘num3’ as the greatest number.
- End program
C Program to Find Largest of Three Numbers using Else If Statement
#include <stdio.h> int main() { int num1, num2, num3; printf(" Enter the number1 = "); scanf("%d", &num1); printf("\n Enter the number2 = "); scanf("%d", &num2); printf("\n Enter the number3 = "); scanf("%d", &num3); if (num1 > num2) { if (num1 > num3) { printf("\n Largest number = %d \n",num1); } else { printf("\n Largest number = %d \n",num3); } } else if (num2 > num3) { printf("\n Largest number = %d \n",num2); } else { printf("\n Largest number = %d \n",num3); } return 0; }
The output of the above c program; as follows:
Enter the number1 = 10 Enter the number2 = 25 Enter the number3 = 65 Largest number = 65
C Program to Find Largest of Three Numbers using Ternary Operator
#include <stdio.h> int main() { int num1, num2, num3, largest; printf(" Enter the number1 = "); scanf("%d", &num1); printf("\n Enter the number2 = "); scanf("%d", &num2); printf("\n Enter the number3 = "); scanf("%d", &num3); largest =((num1>num2 && num1>num3)?num1: (num2>num3)?num2:num3); printf("Largest number = %d \n",largest); return 0; }
The output of the above c program; as follows:
Enter the number1 = 15 Enter the number2 = 25 Enter the number3 = 35 Largest number = 35
C Program to Find Largest of Two Numbers using Nested If Else Statement
#include <stdio.h> int main() { int num1, num2, num3; printf("Enter 1st number : "); scanf("%d", &num1); printf("\nEnter 2nd number : "); scanf("%d", &num2); printf("\nEnter 3rd number : "); scanf("%d", &num3); if(num1 >= num2) { if(num1 >= num3) { printf("\n%d is largest number", num1); } else { printf("\n%d is largest number", num3); } } else { if(num2 >= num3) { printf("\n%d is largest number", num2); } else { printf("\n%d is largest number", num3); } } return 0; }
The output of the above c program; as follows:
Enter 1st number : 85 Enter 2nd number : 65 Enter 3rd number : 78 85 is largest number