Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
3.
Algorithm
4.
Approach 1: Using Nested If Else statement
4.1.
Program:
4.2.
Input:
4.3.
Output:
5.
Approach 2: Using If-Else if Ladder
5.1.
Program:
5.2.
Input 1:
5.3.
Output:
5.4.
Input 2:
5.5.
Output:
6.
Approach 3: Using Ternary Operator
6.1.
Program 1:
6.2.
Input:
6.3.
Output:
6.4.
Program 2:
6.5.
Input:
6.6.
Output:
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

Java Program to Find the Largest of Three Numbers

Author Pankhuri Goel
0 upvote
Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

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. 

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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.

Program:

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);
            }
        }
    }
}

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.

Program:

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.");
        }
    }
}

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.

Program 1:

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);
    }
}

Input:

Output:

Program 2:

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);
    }
}

Input:

Output:

Must Read: Java System Out Println

Must Read Conditional Statements in Java

FAQs

  1. 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.
     
  2. 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 ;
     
  3. 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. 
     
  4. 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.

Key Takeaways

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!

Previous article
Java Program to Display Even Numbers from 1 to 100
Next article
How to Sort List in Java
Live masterclass