Questions: Write a program whose inputs are three integers, and whose output is the smallest of the three values. Ex: If the input is: 715 3 the output is: 3

Write a program whose inputs are three integers, and whose output is the smallest of the three values.
Ex: If the input is:
715 3
the output is:
3
Transcript text: 2.22 LAB: Smallest number Write a program whose inputs are three integers, and whose output is the smallest of the three values. Ex: If the input is: $715 \quad 3$ the output is: 3 LAB ACTIVITY 2.22.1. LAB Smallest number $0 / 10$ LabProgram.java Load default template... ``` import java.util.Scanner; public class LabProgram { public static void main(String[] args) { * Type your code here. */ } } ```
failed

Solution

failed
failed

Solution Steps

Step 1: Import the Scanner class

To read input from the user, we need to import the Scanner class from the java.util package.

import java.util.Scanner;
Step 2: Create the main class and method

Define the main class LabProgram and the main method where the program execution will begin.

public class LabProgram {
    public static void main(String[] args) {
Step 3: Initialize the Scanner and read inputs

Create a Scanner object to read the three integer inputs from the user.

        Scanner scnr = new Scanner(System.in);
        int num1 = scnr.nextInt();
        int num2 = scnr.nextInt();
        int num3 = scnr.nextInt();
Step 4: Determine the smallest number

Use conditional statements to find the smallest of the three numbers.

        int smallest = num1;
        if (num2 < smallest) {
            smallest = num2;
        }
        if (num3 < smallest) {
            smallest = num3;
        }
Step 5: Output the smallest number

Print the smallest number to the console.

        System.out.println(smallest);
    }
}

Final Answer

Here is the complete code:

import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int num1 = scnr.nextInt();
        int num2 = scnr.nextInt();
        int num3 = scnr.nextInt();

        int smallest = num1;
        if (num2 < smallest) {
            smallest = num2;
        }
        if (num3 < smallest) {
            smallest = num3;
        }

        System.out.println(smallest);
    }
}
Was this solution helpful?
failed
Unhelpful
failed
Helpful