Introduction
This is a tutorial for smallest number in java. The program takes two numbers from user and decides the smallest number and prints it. The program is extendable. Go enjoy the program. Lets begin………
Program for smallest number in java.
//import Scanner as we require it. import java.util.Scanner; // the name of our class its public public class SmallestNo { //void main public static void main (String[] args) { //declare int int a,b; //print message System.out.println("Enter two numbers:"); //Take input Scanner input = new Scanner(System.in); a = input.nextInt(); b = input.nextInt(); //if else statement if(a<b) System.out.println(a+" is smallest"); else System.out.println(b+" is smallest"); } }
Output
Enter two numbers:
12
54
12 is smallest
How it works
- The program prints the message to enter two number.
- The user enters two numbers.
- If-else statement decides the smallest number and prints it.
Extending it
The program can be extended by using more numbers and finding the smallest smallest number.
- Add system.out.println() function to print the number.
At the end.
You learnt creating the Java program for Smallest number. So now enjoy the program.
Please comment on the post and share it.
And like it if you liked.