Find Largest of Three Numbers
Problem Statement
Write a program to find the largest of three numbers entered by the user.
This is a basic programming problem that helps beginners understand comparison operators and conditional statements. It is also commonly asked in coding interviews.
Logic Explanation
Take three numbers as input
Compare the first number with the second and third
If the first number is greater than both, it is the largest
Otherwise, check which of the remaining two numbers is greater
Print the largest number
Algorithm
Start
Read three numbers
a,b, andcIf
a >= banda >= c, printaElse if
b >= aandb >= c, printbElse, print
cStop
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
printf("Largest number is %d", a);
else if (b >= a && b >= c)
printf("Largest number is %d", b);
else
printf("Largest number is %d", c);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
if (a >= b && a >= c)
cout << "Largest number is " << a;
else if (b >= a && b >= c)
cout << "Largest number is " << b;
else
cout << "Largest number is " << c;
return 0;
}
import java.util.Scanner;
class LargestOfThree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if (a >= b && a >= c)
System.out.println("Largest number is " + a);
else if (b >= a && b >= c)
System.out.println("Largest number is " + b);
else
System.out.println("Largest number is " + c);
}
}
a = int(input("Enter first number: "))
b = int("Enter second number: ")
c = int("Enter third number: ")
if a >= b and a >= c:
print("Largest number is", a)
elif b >= a and b >= c:
print("Largest number is", b)
else:
print("Largest number is", c)
Input
22 11 30
Output
Largest number is 30
Important Notes
This program uses logical operators (
&&,and)Works correctly even if two numbers are equal
Can be extended to find the largest among N numbers
Interview Tip
This problem tests your understanding of conditional statements and logical operators.
Conclusion
In this program, we learned how to compare three numbers and find the largest one using simple if-else conditions. This logic forms the base for solving more complex comparison problems in programming.
Practice more such problems on EasyHaiCode 🚀
Happy Coding 😊