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.

find the largest number among three numbers

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

Operator in C++ language

Single dimension Array in C++ language

For loop in C++ language

If statements in C++

Data type in C++ programming language

Two dimension array in C++ language

Three dimension array in C++ language

 

Operator in Java language

For loop in Java language

If statements in Java language

Data type 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

For loop in C language 

If statements in C language

Data type in C language

Single dimension array in C language

Two dimension array in C language

Three dimension array in C language

 

 

Program to find largest number among three numbers in Cpp
Cpp program to find largest elements of an Array
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

Explanation of one dimensional array (Data structure)

Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…

1 day ago

Python program to calculate the sum of odd and even numbers in a list

Python program to calculate the sum of odd and even numbers in a list In…

4 days ago

Python code to Calculate sum of odd and even in a list

Python code to Calculate sum of odd and even in a list In this tutorial,…

1 week ago

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 weeks ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

1 month ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

2 months ago