Java Program to Check Largest Among Three Numbers

by | Jan 2, 2023 | Java, Java Programs

Problem Statement

Create a program that returns the largest among three numbers when three numbers, namely x, y, and z, are passed as inputs to the program.

Example: As a simple example, consider the case where three numbers, 7, 20, and 56, have been inputted into a program. The output from the program should be 56.

Input: x = 7, y = 20, z = 56
Output: 56, where the value is stored in the variable ‘z’.

Algorithm for the largest of the three numbers program

  1. Start
  2. Read the three numbers as A, B, and C.
  3. Check for the condition if A greater than B.
  4. If A is greater than B, check if A is greater than C.
  5. If A greater than C, then return A as the largest number out of the three.
  6. If B is greater than C, check if B is greater than C.
  7. If B is greater than C, then B is the largest number of the three. If B is less than C, then C is the largest number out of the three.
  8. Print the largest number, A, B, or C, as follows.
  9. End

Approaches:

  • Using Ternary operator
  • Using if-else

Approach 1: Using Ternary Operator

The syntax for the conditional operator is given as follows:

ans = (conditional expression) ? execute if true : execute if false

  • The statement before the colon (:) is executed if the condition is true.
  • The statement after the colon (:) is executed if the condition is false.

largest = z > (x > y ? x : y) ? z(( x > y ? x : y));

Illustration:

x = 5, y = 10, and z = 3

largest = 3 > (5 > 10 ? 5:10) ? 3: ((5 > 10 ? 5 : 10));
largest = 3 > 10 ? 3 : 10
largest = 10

Code for the program

import java.io.*;
class Demo
{
static int biggest (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=5;
b = 10;
c = 3;
largest = biggest (a , b, c);
System.out.println ( “largest number of the three is” + biggest);
}
}

 

OUTPUT

Largest number of the three is 10.

Approach 2: Using If-else Statements

In this approach, we will use the more widely used technique to compare values using the if-else statement. Almost all major programs contain several if-else statements and even nested if-else blocks. If else statement in this code will check the three numbers provided to the program as inputs. The first if statement here will check if the first number is greater than the other two or not. If true, the first number will be printed out as the largest among the three. The same condition will also be implemented for the second number. If both conditions return false, this means that the third number is the largest number out of the three.

Sample Code

import java.io.*;
class Demo
{
static int biggest (int x, int y, int z)
{
if (x > = y && x > = z)
return x;
else if (y > = x && y > = z)
return y;
else return z;
}
public static void main (String args [])
{
int a , b, c, largest;
a = 5;
b = 10;
c = 3
largest = biggest (a , b, c);
System.out.println ( “largest number of the three is” + biggest);
}
}

 

OUTPUT

Largest number of the three is 10.

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.