Skip to main content

Command Palette

Search for a command to run...

Greatest of Three Numbers program

Updated
Greatest of Three Numbers program
M

IIIT - N Software Geek. Startup Enthusiast. Build for the Future.

Explanation:

Step 1: First, we need to import the Scanner class object for taking input from the user.

Step 2: we need to take 3 variables that are num1, num2, and num3 and store the user input values in those variables.

Step 3: we need to compare input values with one another to check whether it is greater than another number or not.

Step 4: First we need to check num1 is > num2 and num1 > num3 if this condition is true then num1 is the greatest number.

Step 5: If the first condition is false then it will move to the second else if the condition that is num2 is > num1 and num2 > num3 if the condition is true then num2 is the greatest number.

Step 6: if the first and second conditions are false then it will come to the else block condition that prints num3 is the greatest number.

Java Code :

// take 3 numbers and prints max
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter First Number : ");
        int num1 = sc.nextInt();

        System.out.print("Enter Second Number : ");
        int num2 = sc.nextInt();

        System.out.print("Enter Third Number : ");
        int num3 = sc.nextInt();

        if (num1>num2 && num1>num3){
            System.out.println(num1+" is Greater");
        }
        else if(num2 > num1 && num2 > num3){
            System.out.println(num2 + " is Greater");
        }
        else{
            System.out.println(num3 + " is Greater");
        }
    }
}

C++ code:

#include<iostream>
using namespace std;

int main(){
    int num1,num2,num3;
    cout<<"Enter First number : ";
    cin>>num1;
    cout<<"Enter Second Number : ";
    cin>>num2;
    cout<<"Enter Third Number : ";
    cin>>num3;

    if(num1 > num2 && num1 > num3){
        cout<<num1<<" is greater"<<endl;
    }
    else if(num2 > num1 && num2 > num3){
        cout<<num2<<" is greater"<<endl;
    }
    else{
        cout<<num3<<" is greater"<<endl;
    }
}

Python code:

num1 = int(input("Enter First number "))
num2 = int(input("Enter Second number "))
num3 = int(input("Enter Third number "))

if num1> num2 and num1 > num3:
    print(num1 ," is greater ")
elif num2> num1 and num2 > num3:
    print(num2," is greater")
else:
    print(num3," is greater")

More from this blog