Table of contents
1.
Introduction
2.
Problem Statement
3.
Algorithm
4.
Approach 1: Using Nested If Else statement
4.1.
Implementation:
4.2.
Java
4.2.1.
Input:
4.2.2.
Output:
5.
Approach 2: Using If-Else if Ladder
5.1.
Implementation:
5.2.
Java
5.2.1.
Input 1:
5.2.2.
Output:
5.2.3.
Input 2:
5.2.4.
Output:
6.
Approach 3: Using Ternary Operator
6.1.
Implementation 1
6.2.
Java
6.2.1.
Input:
6.2.2.
Output:
6.3.
Implementation 2
6.4.
Java
6.4.1.
Input:
6.4.2.
Output:
7.
Frequently Asked Questions
7.1.
Briefly describe the four basic terminologies of Java.
7.2.
Describe if-else if ladder.
7.3.
Name the various operators available in Java.
7.4.
What is a ternary operator?
8.
Conclusion
Last Updated: Jul 9, 2024
Easy

Java Program to Find the Largest of Three Numbers

Author Pankhuri Goel
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A number is an arithmetical value representing a certain quantity used in counting and calculating. It is expressed as a word, symbol, or figure. These numbers have been in use for centuries. 

They have many fun qualities that help with pattern recognition and algorithm development. Thus, making them one of the most exciting things to play around within interviews and various questions. 

algorithm to find largest of 3 numbers

In this article, we will use these exciting properties to find the largest of the three numbers. Let’s see how!

Also Read About, Multithreading in java

Problem Statement

We are given three numbers, x, y, and z. Our aim is to find the largest number among these three. 

Now, we can follow various approaches to solve this problem but first, let us see the algorithm to find the largest number.

Algorithm

To solve this problem, we may follow the following algorithm:

  1. Start
  2. Read the three numbers to be compared, i.e., x, y, and z.
  3. Check if x is greater than y 
    1.  If true, check if x is greater than z 
      1. If true, print the value of x.
      2. If false, print the value of z.
    2. If false, check if y is greater than z
      1. If true, print the value of y.
      2. If false, print the value of z.
  4. End

 

The flow chart describing the algorithm’s control flow is as follows: 
 

Approach 1: Using Nested If Else statement

In this approach, we are using nested if-else statements. The if statement checks if x is greater than y; if true, it proceeds into the first block of statements. In this block, we check whether x is greater than z or not and proceed accordingly. Now, if the initial condition, i.e., x > y fails, we move into the second block of statements. In this block, we again check if y is greater than z or not and then proceed accordingly.

The code, input and output of this approach are following.

Implementation:

  • Java

Java

import java.util.Scanner;
public class Biggest_Number
{
   public static void main(String[] args)
   {
       int x, y, z;
       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 )
       {
           if ( x > z )
           {
               System.out.println("Largest number: " + x);
           }
           else
           {
               System.out.println("Largest number: " + z);
           }
       }
       else
       {
           if ( y > z )
           {
               System.out.println("Largest number: " + y);
           }
           else
           {
               System.out.println("Largest number: " + z);
           }
       }
   }
}
You can also try this code with Online Java Compiler
Run Code

Input:

Output:

Practice it on online java compiler for better understanding.

Approach 2: Using If-Else if Ladder

In this approach, we are using if-else if statements. 

  • The if statement checks if x is greater than y and z; if true, it proceeds and prints x. 
  • In the else if statement, we check if y is greater than x and z. If true, it proceeds and prints y. 
  • Then in the next else if statement, we check if z is greater than x and y if true, it proceeds and prints z. 
  • If all the statements are false, the else statement block is executed, stating that at least two of the numbers are the same. 

The code, input and output of this approach are following.

Implementation:

  • Java

Java

import java.util.Scanner;
public class Biggest_Number
{
   public static void main(String[] args)
   {
       int x, y, z;
       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("Largest number: " + x);
       }
       else if ( y > x && y > z )
       {
           System.out.println("Largest number: " + y);
       }
       else if ( z > x && z > y )
       {
           System.out.println("Largest number: " + z);
       }
       else
       {
           System.out.println("All the numbers are not distinct.");
       }
   }
}
You can also try this code with Online Java Compiler
Run Code

Input 1:

Output:

Input 2:

Output:

Also check out Addition of Two Numbers in Java here.

Approach 3: Using Ternary Operator

In this approach, we use the ternary operator. The syntax of which is as follows:

res = ( conditional expression ? value1 : value2 ) ;

Where, if the conditional expression is true, value1 is stored in res, and if not, value2 is stored.

This can be done in 2 ways, both of which are mentioned below. In a first way, we use the ternary operator two times. However, in the second method, we use the nested ternary operator.

The code, input and output of this approach are following.

Implementation 1

  • Java

Java

import java.util.Scanner;
public class Biggest_Number
{
   public static void main(String[] args)
   {
       int x, y, z, largest, temp;
       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();
       temp = ( x > y ? x : y ) ;
       largest = ( z > temp ? z : temp ) ;
       System.out.println("Largest number: " + largest);
   }
}
You can also try this code with Online Java Compiler
Run Code

Input:

Output:

Implementation 2

  • Java

Java

import java.util.Scanner;
public class Biggest_Number
{
   public static void main(String[] args)
   {
       int x, y, z, largest;
       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();
       largest = ( z > ( x > y ? x : y ) ? z : ( x > y ? x : y ) ) ;
       System.out.println("Largest number: " + largest);
   }
}
You can also try this code with Online Java Compiler
Run Code

Input:

Output:

Must Read: Java System Out Println

Must Read Conditional Statements in Java

Frequently Asked Questions

Briefly describe the four basic terminologies of Java.

The basic terminology of Java is described as follows:
Class: The class is a blueprint (plan) of the instance of a class (object). It is a template that outlines the data and behaviour connected to its instance. 
Object: An object is a class's instance. It's an entity with behaviour and state.
Method: The method describes how an object behaves.
Instance variables: Each object has a collection of instance variables that are unique to it. The values allocated to these instance variables usually determine the state of an object.

Describe if-else if ladder.

The if-else-if ladder in Java is used to choose amongst numerous possibilities. The if statements are executed in order from top to bottom. When one of the if conditions are true, the statement corresponding with that if is executed, and the rest of the ladder is skipped. If none of the conditions is met, the final else sentence is executed.

Syntax: if (condition)
   statement_1 ;
else if (condition)
   statement_2 ;
.
.
else
   statement ;

Name the various operators available in Java.

Java has a variety of operators that can be utilised depending on the situation. They're categorised based on the features they offer. The following are a few examples of the various types: Arithmetic Operators, Unary Operators, Assignment Operators, Relational Operators, Logical Operators, Ternary Operators, Bitwise Operators, Shift Operators and instance of operators. 

What is a ternary operator?

The only conditional operator in Java that takes three operands is the ternary operator. It's a one-liner alternative to the if-then-else statement. It is widely used in Java programming. Although the conditional operator follows the same algorithm as the if-else statement, it takes up less space and aids in the shortest possible writing of if-else statements.

Conclusion

This article taught us how to find the largest among three numbers in Java. We saw how to solve this problem using nested if-else statement, if-else if ladder and ternary operator.

We hope this blog has helped you enhance your knowledge. If you would like to learn more, check out our articles on Introduction to Java and Decision Statements in Java. If you want to learn more about the basics of Java, follow this guided path Basics Of Java. Do upvote our blog to help other ninjas grow.

Read about Bitwise Operators in C and Strong number in c  here.

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more!

Happy Reading!

Live masterclass