Greatest of the Two 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.

Program Logic

  1. Input:
    • The user is prompted to enter two numbers.
  2. Initialization:
    • Two variables, num1 and num2, are declared to store the input numbers.
  3. Comparison:
    • The program compares the values of num1 and num2 using an if statement.
  4. Output:
    • If num1 is greater than num2, the program prints num1 as the greatest number.
    • Otherwise, if num2 is greater than or equal to num1, the program prints num2 as the greatest number.
  5. Termination:
    • The program ends.

Explanation of the Logic:

  • The program begins by asking the user to input two numbers, num1 and num2.
  • Next, it compares the values of num1 and num2 to determine which one is greater.
  • If num1 is greater than num2, the program prints num1 as the greatest number.
  • If num2 is greater than or equal to num1, the program prints num2 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