Skip to content
Advertisement

How to Find Smallest Number Based On User Input – Java

I’m trying to find the smallest number from the inputs (5 inputs) of users. However, every time I click run and write down the numbers, the smallest number will always be “0”. Here is my code:

JavaScript

Advertisement

Answer

Algorithm:

  1. Input the first number and assume it to be the smallest number.
  2. Input the rest of the numbers and in this process replace the smallest number if you the input is smaller than it.

Demo:

JavaScript

A sample run:

JavaScript

Note: Do not close a Scanner(System.in) as it also closes System.in and there is no way to open it again.

Advertisement