2.7 Code Practice: Question 1

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

Can someone please help me with this because I’ve been struggling all day

Respuesta :

Answer:

You didn't specify the programming language

I'll answer using Python

nums = []

for i in range(1,4):

     inp = int(input("User Input: "))

     nums.append(inp)

nums.sort()

print(nums[-1])

Explanation:

I made use of list in answering this question.

Start by declaring an empty list

nums = []

Use iteration to enter the list elements

for i in range(1,4):

     inp = int(input("User Input: "))

     nums.append(inp)

Sort the list

nums.sort()

Then print the element on the last index

print(nums[-1])

The code below is in Java

It uses if-else structure to find out the largest number among three numbers given by the user.

Comments are used to explain each line of the code

//Main.java

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

    //Create a Scanner object to be able to get input

    Scanner input = new Scanner(System.in);

   

    //Declare the variables

    int number1, number2, number3, largest;

   

    //Ask the user to enter three numbers

 System.out.print("Enter the first number: ");

 number1 = input.nextInt();

 System.out.print("Enter the second number: ");

 number2 = input.nextInt();

 System.out.print("Enter the third number: ");

 number3 = input.nextInt();

 

 /*

     Check if the first number is greater than or equal to the second and third number.

     If it is, the largest is the first one.

     Otherwise, check if the second number is greater than or equal to the first and third number.

     If it is, the largest is the second one.

     If the previous conditions are not satisfied that means third is the largest one

 */

 if(number1 >= number2 && number1 >= number3)

     largest = number1;

 else if(number2 >= number1 && number2 >= number3)

     largest = number2;

 else

     largest = number3;

 

 //Print the largest number  

 System.out.println("The largest number is " + largest);

}

}

You may see a similar question in the following link:

brainly.com/question/17132559