Find elements

Java Program to smallest and largest among three numbers

Java Program to smallest and largest among three numbers

In this tutorial, we will discuss Java Program to smallest and largest among three numbers

In this program, we will learn a simple concept of the program to find the smallest and largest number among the three numbers in the Java programming language.

In this topic, we learn how to find the smallest and largest number from given three numbers using if statements.

Find smallest and largest among float numbers

import java.util.Scanner;
class Small_Large1{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1=scan.nextInt();//get input from user for num1
System.out.print("Enter the second number: ");
int num2=scan.nextInt();//get input from user for num2
System.out.print("Enter the third number: ");
int num3=scan.nextInt();//get 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);
}

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 largest number is: "+num3);
}
}
}

When the above code is compiled and executed, it produces the following results

Enter the first number: 56
Enter the second number: 34
Enter the third number: 78

The Smallest number is: 34
The largest number is: 78

 

 

Find smallest and largest among float numbers

import java.util.Scanner;
class Small_Large{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the first number: ");
float num1=scan.nextFloat();//get input from user for num1
System.out.print("Enter the second number: ");
float num2=scan.nextFloat();//get input from user for num2
System.out.print("Enter the third number: ");
float num3=scan.nextFloat();//get 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);
}

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 largest number is: "+num3);
}
}
}

When the above code is compiled and executed, it produces the following results

Enter the first number: 12.34
Enter the second number: 34.56
Enter the third number: 56.78

The smallest number is 12.34
The Largest number is 56.78

 

Suggested for you

Operator in Java language

Data type in Java language

if statements in Java language

 

 

Java Program to smallest among three numbers
C Program to display smallest among three numbers
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

1 day ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago