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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago