Find Largest of Two Numbers
Problem Statement
Write a program to find the largest of two numbers entered by the user.
This is a very common beginner-level programming problem and is frequently asked in coding interviews and exams.
Logic Explanation
Take two numbers as input from the user
Compare both numbers
If the first number is greater, print it
Otherwise, print the second number
Algorithm
Start
Read two numbers
aandbIf
a > b, printaas the largestElse, print
bas the largestStop
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if (a > b)
printf("Largest number is %d", a);
else
printf("Largest number is %d", b);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b;
if (a > b)
cout << "Largest number is " << a;
else
cout << "Largest number is " << b;
return 0;
}
import java.util.Scanner;
class LargestNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
if (a > b)
System.out.println("Largest number is " + a);
else
System.out.println("Largest number is " + b);
}
}
Python Program
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
print("Largest number is", a)
else:
print("Largest number is", b)
Input
15 10
Output
Largest number is 15
Important Notes
This program uses if-else condition
Works for positive and negative numbers
Forms the base for solving largest of three numbers
Interview Tip
“Finding the largest number is often used to test your understanding of conditions and comparison operators.”
Conclusion
In this program, we learned how to compare two numbers and find the largest one using simple logic.
This problem helps beginners understand decision-making statements like if-else, which are very important in programming.