Write a program that computes and outputs the smaller of two

Write a program that computes and outputs the smaller of two numbers entered by the user. In this part you are NOT allowed to use the built-in method Math.min().

the program has to be written in java

Solution

Answer:


package smallestnumber;
import java.util.Scanner;

public class SmallestNumber {

public static void main(String[] args) {

Scanner inputValues= new Scanner(System.in); //For user input we use scanner object
int firstNum , secondNum;
System.out.println(\"Please enter the first number: \"); // prompt user to enter first value
firstNum = inputValues.nextInt(); // get the first user input
System.out.println(\"Please enter the second number: \"); // prompt user to enter second value
secondNum = inputValues.nextInt(); // get the second user input
if(firstNum < secondNum){ // check if the first input value is smaller than the second one
System.out.println(\"The smallest number is : \" + firstNum); // print first number as the smallest value
}else if(secondNum < firstNum){ // check if second number is smaller
System.out.println(\"The smallest number is : \" + secondNum); // print second number as the smallest value
}else{
System.out.println(\"Both the numbers are equal.\"); // else print both numbers are equal
}
}
  
}

Sample Input:

-90, -25

Sample Output:

The smallest number is : -90

Write a program that computes and outputs the smaller of two numbers entered by the user. In this part you are NOT allowed to use the built-in method Math.min()

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site