If statement

Java program to Find Largest of three numbers

Java program to Find Largest of three numbers

In this article, we will discuss the concept of Java program to Find Largest of three numbers

In this post, we are going to learn how to write a program to find largest number out of three numbers using different methods in Java program.

Code to find largest numbers

Code to find largest numbers  using if statements

In this code, we will find largest number out of three numbers using if statements in Java language

Program 1

import java.util.Scanner;
class Large_Of_Three{
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
System.out.print("Enter the second number: ");
int num2=scan.nextInt();//get input from user
System.out.print("Enter the third number: ");
int num3=scan.nextInt();//get input from user

if(num1>=num2 && num1>=num3){
    System.out.println("\n The largest number is: "+num1);
  //Checking the num1 is largest
}
if(num2>=num1 && num2>=num3){
    System.out.println("\n The largest number is: "+num2);
  //Checking the num2 is largest
}
if(num3>=num1 && num3>=num2){
    System.out.println("\n The largest number is: "+num3);
  //Checking the num3 is largest
}
}
}

When the above code is executed, it produces the following result

Enter the first number: 1877
Enter the second number: 9877
Enter the third number:: 456
The largest number is: 9877

 

Code to find largest numbers  using Array

In this code, we will find largest number out of three numbers using Array in Java language

Program 2

import java.util.Scanner;
class Large_Num_Array{
public static void main (String args[]){
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number of elements in an array: ");
int max;
int n=scan.nextInt();//get input from user for array length
int arr[]=new int[n]; //declaring an array of n elements

//for loop takes input from user
for(int i=0; i<n; i++){
   System.out.print("Enter the element "+(i+1)+": ");
   arr[i]=scan.nextInt();//takes input from user for array
   }
   max=arr[0];
   for(int i=0; i<n; i++){
     if(max<arr[i]){
     max=arr[i];
   }
   }
   System.out.print("\nThe largest value is: "+max);
}//display result on the screen
}

When the above code is executed, it produces the following result

Enter the number of elements in an array:: 3
Enter the element 1: 9876
Enter the element 2: 6543
Enter the element 1: 4321

The largest value is: 9876

 

Code to find largest numbers  using Nested if statements

In this code, we will find largest number out of three numbers using Nested if statements in Java language

Program 3

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 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) {
     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 executed, it produces the following result

Enter the first number: 765
Enter the second number: 654
Enter the third number: 543

The largest number is: 765

 

 

Code to find largest numbers  using if else-if statements

In this code, we will find largest number out of three numbers using if else if statements in Java language

Program 4

import java.util.Scanner;
class Large_Of_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 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 largest number is: "+num1);
}//Check the num1 is largest
else if(num2>=num1 && num2>=num3){
    System.out.println("\n The largest number is: "+num2);
}//Check the num2 is largest
else{
    System.out.println("\n The largest number is: "+num3);
}
}
}

When the above code is executed, it produces the following result

Enter the first number: 654
Enter the second number: 789
Enter the third number: 132

The largest number is: 789

 

 

Code to find largest numbers  using ternary operator

In this code, we will find largest number out of three numbers using ternary operator in Java language

Program 5

import java.util.Scanner;
class Large_Three_Conditional{
public static void main (String args[]){
int result;
Scanner scan=new Scanner(System.in);
System.out.print("Enter the first number: ");
//Ask input from the user
int num1=scan.nextInt();//Reading input from the user for num1
System.out.print("Enter the second number: ");
int num2=scan.nextInt();//Reading input from the user for num2
System.out.print("Enter the third number: ");
int num3=scan.nextInt();//Reading input from the user for num3
result=num3>(num1>num2?num1:num2)?num3:((num1>num2)? num1:num2);
System.out.println("Largwst number is: "+result);

}
}

When the above code is executed, it produces the following result

Enter the first number: 2564
Enter the second number: 8769
Enter the third number: 5674
The largest number is: 8769

 

Program 6

import java.util.Scanner;
class Large_Three_Conditiona2{
public static void main (String args[]){

Scanner scan=new Scanner(System.in);
System.out.print("Enter the first number: ");
//Ask input from the user
int num1=scan.nextInt();//Reading input from the user for num1
System.out.print("Enter the second number: ");
int num2=scan.nextInt();//Reading input from the user for num2
System.out.print("Enter the third number: ");
int num3=scan.nextInt();//Reading input from the user for num3
int temp=(num1>num2)? num1:num2;
int largest= num3>temp?num3:temp;
System.out.println("Largest number is: "+largest);

}
}

When the above code is executed, it produces the following result

Enter the first number: 10435
Enter the second number:6578
Enter the third number:3456
The largest number is: 10435

 

 

Code to find largest numbers  using method

In this code, we will find largest number out of three numbers using Method in Java language

Program 7

import java.util.Scanner;
class Large_Of_ThreeMethod1{
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
find_Largest(num1,num2,num3);//Calling the method
}

static void find_Largest(int num1, int num2, int num3){
  //User- defined method definition
  if(num1>=num2 && num1>=num3){
    System.out.println("\n The largest number is: "+num1);
}//Check the num1 is largest
else if(num2>=num1 && num2>=num3){
    System.out.println("\n The largest number is: "+num2);
}//Check the num2 is largest
else{
    System.out.println("\n The largest number is: "+num3);
}
}
}

When the above code is executed, it produces the following result

Enter the first number: 145
Enter the second number 783
Enter the third number: 567

The largest number is: 783


 

Program 8

import java.util.Scanner;
class Large_Of_ThreeMethod2{
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
find_Largest(num1,num2,num3);//Calling the method
}

static void find_Largest(int num1, int num2, int num3){
  //User- defined method definition
  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 executed, it produces the following result

Enter the first number: 618
Enter the second number 980
Enter the third number: 325

The largest number is: 980

 

Suggested post

Operator in Java language

Data type in Java language

if statement in Java language

Nested if statements in Java language

Method in Java language

Array in Java language

 

Similar post

Java program to find middle of three

C program to find middle of three

C++ program to find middle of three

Python program to find middle of three

 

Java program to Find largest of three numbers

C program to Find largest of three numbers

C++ program to Find largest of three numbers

Python program to Find largest of three numbers

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.