Ask Question
Today, 04:38

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

+2
Answers (1)
  1. Today, 05:40
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers