Do you think IIT Guwahati certified course can help you in your career?
No
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!
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:
Start
Read the three numbers to be compared, i.e., x, y, and z.
Check if x is greater than y
If true, check if x is greater than z
If true, print the value of x.
If false, print the value of z.
If false, check if y is greater than z
If true, print the value of y.
If false, print the value of z.
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
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
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
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
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.