Write a program to find the greatest of three numbers in Java
In this post, you will learn how to find the largest of three numbers using the Java programming language. Such a type of programming can improve the thinking power of a new developer to tackle a given issue. There are different ways to find the largest of the three numbers. Here, we have mentioned most of them-
Find the largest number using if-else statement
In the given Java program, we are taking input from the user. The user enters three numbers and program finds the biggest amongst those three numbers using the if..else if..else statement.
import java.util.Scanner;
public class Largest_Number
{
public static void main(String[] args)
{
int x, y, z;
// Getting three integer values from user
Scanner s = new Scanner(System.in);
System.out.print("Enter the first number:");
x = s.nextInt();
System.out.print("Enter the second number:");
y = s.nextInt();
System.out.print("Enter the third number:");
z = s.nextInt();
if(x > y && x > z)
{
System.out.println("The largest number is: "+x);
}
else if(y > z)
{
System.out.println("The largest number is: "+y);
}
else
{
System.out.println("The largest number is: "+z);
}
}
}
Output of the above code:
Enter the first number: 42
Enter the second number: 12
Enter the third number: 87
The largest number is: 87
Find the largest number using nested if-else statement
Here, we have used the nested if-else statements to find the greatest number using the Java programming language.
public class Largest_Number {
public static void main(String[] args) {
int num1 = 21, num2 = 48, num3 = 51;
if(num1 >= num2) {
if(num1 >= num3)
System.out.println(num1 + " is the largest number.");
else
System.out.println(num3 + " is the largest number.");
} else {
if(num2 >= num3)
System.out.println(num2 + " is the largest number.");
else
System.out.println(num3 + " is the largest number.");
}
}
}
Output of the above code:
51 is the largest number.
Find the largest number using ternary operator
The ternary operator is an operator that exists in some programming languages and takes three operands rather than the typical one or two that most operators use. Here, we have used the ternary operator to find the greatest of three numbers using the Java programming language.
import java.util.Scanner;
public class LargestNumberExample1
{
public static void main(String[] args)
{
int n1, n2, n3, largest, temp;
Scanner sc = new Scanner(System.in);
// Getting three integer values from user
System.out.println("Enter the first number:");
n1 = sc.nextInt();
System.out.println("Enter the second number:");
n2 = sc.nextInt();
System.out.println("Enter the third number:");
n3 = sc.nextInt();
// comparing n1 and n2
// and storing the largest number
// in the temp variable
temp = n1>n1?n1:n2;
// comparing n3 with the temp variable
// storing the result in the largest variable
largest = n3>temp?n3:temp;
System.out.println("The largest number is: "+largest);
}
}
Output of the above code:
Enter the first number: 56
Enter the second number: 83
Enter the third number: 18
The largest number is: 83
Find the largest number using user-defined function
Here, we find the largest of three numbers by defining a user-defined function 'getLargestNumber()' using the Java programming language.
package com.tcc.java.programs;
import java.util.Scanner;
public class MaximumThreeNumbers {
public static void main(String[] args) {
int num1, num2, num3, max;
Scanner scanner;
// Getting three integer values from user
scanner = new Scanner(System.in);
System.out.println("Enter Three Integer: ");
num1 = scanner.nextInt();
num2 = scanner.nextInt();
num3 = scanner.nextInt();
max = getLargestNumber(getLargestNumber(num1, num2), num3);
System.out.println("The largest number is: " + max);
}
/**
* Returns maximum of num1 and num2
*/
public static int getLargestNumber(int num1, int num2) {
if (num1 > num2) {
return num1;
} else {
return num2;
}
}
}
Output of the above code:
Enter Three Integer: 51 42 52
The largest number is: 52
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java