Java Program to smallest and largest among three numbers
- Home
- Find elements
- Java Program to smallest and largest among three numbers
- On
- By
- 0 Comment
- Categories: 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