Program to check the Greatest of Two Integer Number
Definition & Explanation
This program is designed to determine the greatest of two numbers entered by the user. It prompts the user to input two numbers and then compares them to determine which one is greater. It utilizes conditional statements to perform the comparison and outputs the result indicating the greatest number.
More Description
Program Logic
- Input:
- The user is prompted to enter two numbers.
- Initialization:
- Two variables,
num1
andnum2
, are declared to store the input numbers.
- Two variables,
- Comparison:
- The program compares the values of
num1
andnum2
using anif
statement.
- The program compares the values of
- Output:
- If
num1
is greater thannum2
, the program printsnum1
as the greatest number. - Otherwise, if
num2
is greater than or equal tonum1
, the program printsnum2
as the greatest number.
- If
- Termination:
- The program ends.
Explanation of the Logic:
- The program begins by asking the user to input two numbers,
num1
andnum2
. - Next, it compares the values of
num1
andnum2
to determine which one is greater. - If
num1
is greater thannum2
, the program printsnum1
as the greatest number. - If
num2
is greater than or equal tonum1
, the program printsnum2
as the greatest number. - The program then terminates.
This program demonstrates a simple comparison logic where it checks the relationship between two numbers and determines the greatest one among them.
C
Method 1 :
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 > num2) {
printf("The greatest number is: %d\n", num1);
} else {
printf("The greatest number is: %d\n", num2);
}
return 0;
}
Output :
C++
Method 1 :
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
if (num1 > num2) {
cout << "The greatest number is: " << num1 << endl;
} else {
cout << "The greatest number is: " << num2 << endl;
}
return 0;
}
Output :
JAVA
Method 1 :
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int num1, num2;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter two numbers: ");
num1 = scanner.nextInt();
num2 = scanner.nextInt();
if (num1 > num2) {
System.out.println("The greatest number is: " + num1);
} else {
System.out.println("The greatest number is: " + num2);
}
}
}
Output :
Python
Method 1 :
a=12
b=46
if a>b:
print("a is the Greatest Number")
else:
print("b is the greatest Number")
Output :
6