Program to find largest number among three numbers in Java
- Home
- Find elements
- Program to find largest number among three numbers in Java
- On
- By
- 0 Comment
- Categories: Find elements
Program to find largest number among three numbers in Java
Program to find the largest number among three numbers in Java
In this program, we will discuss a simple concept of the program to find the largest number among three numbers in Java programming language.
In this topic, we learn how to find the biggest number from given three numbers in Java programming language. we can use the operator in Java language to find the biggest number of this program.
Using if statements to find the largest number
Among integer numbers
Program 1
import java.util.Scanner; class Large_Three{ public static void main (String args[]){ double max;//variable declaration Scanner scan=new Scanner(System.in); System.out.print("Enter the first number: "); int num1=scan.nextInt();//get input from user for first number System.out.print("Enter the second number: "); int num2=scan.nextInt();//get input from user for second number System.out.print("Enter the third number: "); int num3=scan.nextInt();//get input from user for third number if(num1>=num2 && num1>=num3){//num1 compare with num2 and num3 System.out.println("\n The largest number is: "+num1); } if(num2>=num1 && num2>=num3){//num2 compare with num1 and num3 System.out.println("\n The largest number is: "+num2); } if(num3>=num1 && num3>=num2){//num3 compare with num1 and num2 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:89 Enter the third number: 32 The largest number : 89
Among the floating point
import java.util.Scanner; class Large_Three1{ public static void main (String args[]){ Scanner scan=new Scanner(System.in); System.out.print("Enter the first number: "); double num1=scan.nextDouble();//get input from user for first number System.out.print("Enter the second number: "); double num2=scan.nextDouble();//get input from user for second number System.out.print("Enter the third number: "); double num3=scan.nextDouble();//get input from user for third number if(num1>=num2 && num1>=num3){ System.out.println("\n The largest number is: "+num1); } if(num2>=num1 && num2>=num3){ System.out.println("\n The largest number is: "+num2); } if(num3>=num1 && num3>=num2){ 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: 4.6 Enter the second number:6.5 Enter the third number: 3.7 The largest number is: 6.5
Using if-else statements to find the largest
import java.util.Scanner; class Large_Three1{ 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 first number System.out.print("Enter the second number: "); int num2=scan.nextInt();//get input from user for second number System.out.print("Enter the third number: "); int num3=scan.nextInt();//get input from user for third number if(num1>=num2 && num1>=num3){ System.out.println("\n The largest number is: "+num1); } else if(num2>=num1 && num2>=num3){ System.out.println("\n The largest 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: 45 Enter the second number:67 Enter the third number:80 The largest number is:80
Using nested if statements to find the largest
import java.util.Scanner; class Large_Three2{ 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 first number System.out.print("Enter the second number: "); int num2=scan.nextInt();//get input from user for second number System.out.print("Enter the third number: "); int num3=scan.nextInt();//get input from user for third number if(num1>=num2) { if(num1>=num3){ System.out.println("\n The largest number is: "+num1); } else{ System.out.println("\n The largest number is: "+num3); } } else{ if(num2>=num3){ System.out.println("\n The largest 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: 67 Enter the second number: 99 Enter the tird number: 65 The largest number is: 99
In the above, all programs, Three variables num1,num2,num3 are compared one by one using if related statements to find largest one.
Similar post
largest number of three number in C
largest number of three number in C++
largest number of three number in Python
largest number of three number in C using function
largest number of three number in C++ using function
largest number of three number in Python using function
largest number of three number in Java using method
Suggested for you
Single dimension Array in C++ language
Data type in C++ programming language
Two dimension array in C++ language
Three dimension array in C++ language
If statements in Java language
Single dimension array in Java language
Two dimension array in Java language
Three dimension array in Java language
Operator in C language with example
Single dimension array in C language
Two dimension array in C language
Three dimension array in C language