Ask Question
14 October, 13:10

Write a program that takes three numbers as input and prints the largest.

Sample run:

Enter a number: 20

Enter a number: 50

Enter a number: 5

Largest: 50

+3
Answers (1)
  1. 14 October, 17:01
    0
    Since no programming language is mention following is the Java program for the given problem:

    import java. util. Scanner;

    public class Main{

    public static void main (String[] args) {

    int num1, num2, num3, largest=0;

    Scanner keyboard = new Scanner (System. in);

    //Taking three numbers input from user.

    System. out. println ("Enter a number:");

    num1 = keyboard. nextInt ();

    System. out. println ("Enter a number:");

    num2 = keyboard. nextInt ();

    System. out. println ("Enter a number:");

    num3 = keyboard. nextInt ();

    //Comparing which among three numbers are largest.

    if (num1>num2 && num1>num3)

    {

    largest = num1;

    }else if (num2>num3 && num2>num1)

    {

    largest = num2;

    }

    else

    {

    largest = num3;

    }

    System. out. println ("Largest:"+largest);

    }

    }

    Output:

    Enter a number:

    20

    Enter a number:

    50

    Enter a number:

    5

    Largest:50

    Explanation:

    Above program will store three integers in num1, num2 and num3. Then it will compare if num1 is greater than both num2 and num3. If yes it will assign value of num1 to largest.

    If not it will then check if num2 is greater than both num1 and num3. If yes it assign value of num2 to largest else it assign value of num3 to largest.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a program that takes three numbers as input and prints the largest. Sample run: Enter a number: 20 Enter a number: 50 Enter a number: ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers