If statement

Program to Find Smallest of three numbers in Java

Program to Find Smallest of three numbers in Java

In this article, we will discuss the concept of Program to Find Smallest of three numbers in Java

In this post, we are going to learn how to write a program to find smallest number out of three numbers using different methods in Java program.

Code to find smallest numbers

Code to find smallest numbers  using if statements

In this code, we will find smallest number out of three numbers using if statements in Java language

Program 1

import java.util.Scanner;
class Small_Three_Num{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1=scan.nextInt();//read input from user
System.out.print("Enter the second number: ");
int num2=scan.nextInt();//read input from user
System.out.print("Enter the third number: ");
int num3=scan.nextInt();//read input from user

if(num1<=num2 && num1<=num3){
    System.out.println("\n The smallest number is: "+num1);
}
if(num2<=num1 && num2<=num3){
    System.out.println("\n The smallest number is: "+num2);
}
if(num3<=num1 && num3<=num2){
    System.out.println("\n The smallest number is: "+num3);
}
}
}

When the above code is executed, it produces the following result

Enter the first number: 678
Enter the first number: 435
Enter the first number: 234

The smallest number is: 234

 

Code to find smallest numbers  using if else-if statements

In this code, we will find smallest number out of three numbers using if else-if statements in Java language

Program 2

import java.util.Scanner;
class Small_Three1{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1=scan.nextInt();//read input from user for num1
System.out.print("Enter the second number: ");
int num2=scan.nextInt();///read input from user for num2
System.out.print("Enter the third number: ");
int num3=scan.nextInt();///read input from user for num3

if(num1<=num2 && num1<=num3){
    System.out.println("\n The Smallest number is: "+num1);
}
else if(num2<=num1 && num2<=num3){
    System.out.println("\n The Smallest number is: "+num2);
}
else{
    System.out.println("\n The Smallest number is: "+num3);
}
}
}

When the above code is executed, it produces the following result

Enter the first number: 1234
Enter the first number: 432
Enter the first number: 5678

The smallest number is: 432

 

Code to find smallest numbers  using Nested if statements

In this code, we will find smallest number out of three numbers using Nested if statements in Java language

Program 3

import java.util.Scanner;
class Small_Nested{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1=scan.nextInt();//read input from user for num1
System.out.print("Enter the second number: ");
int num2=scan.nextInt();//read input from user for num2
System.out.print("Enter the third number: ");
int num3=scan.nextInt();//read input from user for num3

if(num1<=num2){
  if(num1<=num3){
    System.out.println("\n The Smallest number is: "+num1);
}
else{
  System.out.println("\n The Smallest number is: "+num3);
}
}
else{
  if(num2<=num3){
    System.out.println("\n The Smallest number is: "+num2);
  }
else{
    System.out.println("\n The Smallest number is: "+num3);
}
}
}
}

When the above code is executed, it produces the following result

Enter the first number: 456
Enter the first number: 213
Enter the first number: 598

The smallest number is: 213

 

Code to find smallest numbers  using ternary operator

In this code, we will find smallest number out of three numbers using Ternary operator in Java language

Program 4

import java.util.Scanner;
class Small_Three_Ternary{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//scanner object for user input
System.out.print("Enter three number: ");
int num1=scan.nextInt();//storing input from user for num1
int num2=scan.nextInt();//storing input from user for num2
int num3=scan.nextInt();//storing input from user for num3
int result=num3<(num1<num2?num1:num2)?num3:((num1<num2)? num1:num2);
System.out.println("Smallest number is: "+result);

}
}

When the above code is executed, it produces the following result

Enter three numbers: 654
876
23

The smallest number is: 23

 

Program 5

import java.util.Scanner;
class Small_Three_Ternary1{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
//scanner object for user input
System.out.print("Enter three number: ");
int num1=scan.nextInt();
//Reading input from user for num1
int num2=scan.nextInt();
//Reading input from user for num2
int num3=scan.nextInt();
//Reading input from user for num3
int temp=((num1<num2)? num1:num2);

int result=num3<temp?num3:temp;
System.out.println("Smallest number is: "+result);

}
}

When the above code is executed, it produces the following result

Enter three numbers: 1021
234
2345

The smallest number is: 234

 

 

Code to find smallest numbers  using Array in Java

In this code, we will find smallest number out of three numbers using array in Java language

Program 6

import java.util.Scanner;
class Small_num{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of elements in an array: ");
int min;
int n=scan.nextInt();
//Takes input from user for array length
int arr[]=new int[n]; 
//declaring an array of given number of element

//for loop takes input from user to n times
for(int i=0; i<n; i++){
   System.out.print("Enter the element "+(i+1)+": ");
   arr[i]=scan.nextInt();//takes input from user for array elemebts
   }
   min=arr[0];
   for(int i=0; i<n; i++){
     if(min>arr[i]){
     min=arr[i];
   }
   }
   System.out.print("\nThe smallest value is: "+min);
}//display result on the screen
}

When the above code is executed, it produces the following result

Enter the number of elements in an array: 3
Enter the element 1: 67
Enter the element 2: 34
Enter the element 3: 56

The smallest value is : 34

 

Code to find smallest numbers  using method in Java

In this code, we will find smallest number out of three numbers using user defined method in Java language

Program 7

import java.util.Scanner;
class Smallest_Num_Method{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter theree number\n");
int num1=scan.nextInt();//reading num1 from user
int num2=scan.nextInt();;//reading num2 from user
int num3=scan.nextInt();;//reading num3 from user
find_Smallest(num1,num2,num3);//calling the method

}
static void find_Smallest(int num1,int num2, int num3){//method definition
  if(num1<=num2 && num1<=num3){
    System.out.println(num1+" is the smallest number");
    
  }
  else if(num2<=num1 && num2<=num3){
    System.out.println(num2+" is the smallest number");
    
  }
  else{
    System.out.println(num3+" is the smallest number");
    
  }
  
}

}

When the above code is executed, it produces the following result

Enter three numbers
345
127
240

127 is the smallest number

 

Program 8

import java.util.Scanner;
class SmallestNum_Method1{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
//create a scanner object for input

System.out.print("Enter the first number\n");
int num1=scan.nextInt();//reading num1 from user
int num2=scan.nextInt();;//reading num2 from user
int num3=scan.nextInt();;//reading num3 from user
//findSmallest(num1,num2,num3);//calling the method
System.out.println(findSmallest(num1,num2,num3)+" is the smallest number");
}
static int findSmallest(int num1,int num2, int num3){//method definition
  if(num1<=num2){
  if(num1<=num3)
    return num1;
    else
    return num3;
  }
  else{
  
  if(num2<=num3)
    return num2;
    
  else
    return num3;
  }
  
}

}

When the above code is executed, it produces the following result

Enter three numbers
1245
1000
5634

1000 is the smallest number

 

Suggested post

Operator in Java language

Data type in Java language

if statement in Java language

Nested if statements in Java language

Method in Java language

Array in Java language

 

Similar post

Java code to find middle of three

C program to find middle of three

C++ program to find middle of three

Python program to find middle of three

 

Java code to Find largest of three numbers

C program to Find largest of three numbers

C++ program to Find largest of three numbers

Python program to Find largest of three numbers

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.