Input 10 numbers and print the smallest using while loop in Java
while Loop - Question 3
In this question, we will see how to input 10 numbers and print the smallest among them in Java programming using while loop. To know more about while loop click on the while loop lesson.
Q3) Write a program in Java to input 10 numbers and print the smallest among them using while loop.
Program
import java.util.Scanner;
public class Q3
{
public static void main(String args[])
{
int i=1,n,sm=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 10 numbers");
while(i<=10)
{
n=sc.nextInt();
if(i==1)
{
sm=n; // store the first number
}
else if(n<sm) // compare the previous stored number with the latest entered number
{
sm=n;
}
i=i+1;
}
System.out.println("Smallest Number = " + sm);
}
}
Output
Enter 10 numbers 12 6 48 95 3 11 27 36 54 89 Smallest Number = 3