Java Programming | Smallest Number

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

  1. The program prints the message to enter two number.
  2. The user enters two numbers.
  3. 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 out of them. Go extend it.

Explanation.

  1. Import the Scanner.
  2. Declare the class as public
  3. Add the void main function
  4. Add system.out.println() function with the message to enter two numbers.
  5. Declare input as Scanner.
  6. Take the inputs and save it in variables.
  7. Add a if-else statement to find the smallest number.
  8. 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.

Sharing options: