Java find largest of three numbers
In this post, you will learn how to find the largest of three numbers using Java programming. Such 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 in Java.
Java find largest of three numbers using if else statement
In the given Java program, we are taking three numbers and the program finds the largest among three numbers using if..else if..else statement.
public class JavaProgram{
public static void main(String[] args) {
int num1 = 52, num2 = 91, num3 = 21;
if( num1 >= num2 && num1 >= num3)
System.out.println(num1+" is the largest Number.");
else if (num2 >= num1 && num2 >= num3)
System.out.println(num2+" is the largest Number.");
else
System.out.println(num3+" is the largest Number.");
}
}
Output of the above code:
91 is the largest Number.
Java find largest of three numbers using nested if statement
Here, we have used the nested if-else statements to find the largest of three numbers in java.
public class JavaProgram{
public static void main(String[] args) {
int num1 = 49, num2 = 105, num3 = 21;
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:
105 is the largest number.
Java find largest of three numbers using ternary operator
In the given example, we are finding the largest of three numbers in Java using the ternary operator.
public class JavaProgram{
// Function to find the largest of three numbers
static int findLargestNo(int x, int y, int z)
{
return z > (x > y ? x : y) ? z : ((x > y) ? x : y);
}
public static void main(String[] args)
{
int a, b, c;
int largest;
a = 75;
b = 105;
c = 82;
// Calling the above function
largest = findLargestNo(a, b, c);
// Printing the largest number
System.out.println(largest + " is the largest number.");
}
}
Output of the above code:
105 is the largest number.
Java find largest of three numbers using user input
In the given Java program, we are taking inputs from the user. The user enters three numbers and the program finds the largest among three numbers by comparing and storing the number in a temp variable.
import java.util.Scanner;
public class JavaProgram{
public static void main(String[] args)
{
int x, y, z, largest, temp;
//object of the Scanner class
Scanner sc = new Scanner(System.in);
// getting user inputs
System.out.println("Enter the first number:");
x = sc.nextInt();
System.out.println("Enter the second number:");
y = sc.nextInt();
System.out.println("Enter the third number:");
z = sc.nextInt();
//comparing x and y and storing the largest number in a temp variable
temp = x>y?x:y;
//comparing the temp variable with z and storing the result in largest variable
largest = z>temp?z:temp;
//printing the largest number
System.out.println(largest+ " is the largest number.");
}
}
Output of the above code:
Enter the first number: 12
Enter the second number: 51
Enter the third number: 29
51 is the largest number.
Related Articles
Number pattern programs in JavaJava 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
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number