[905 views]
There are various methods to find the largest number. Like you can use the maximum function of the programming language directly but the main thing here is understanding how you solve any problem or what is the logic behind the problem and its time complexity. Everyone seeks the shortest time to solve a problem for efficient performance. Coding is all about brainstorming and what was your logic behind any problem. The more you go through problems the better you will be at it.
To solve this problem, you can use only If statements or If and else or nested If and else or using Ternary Operator. These are all of the same type comparing the numbers which each other. The only difference is the way of writing. I will be using the Ternary operator as many of them don't use it but is very useful as with few lines you can write the program.
In this algorithm, we will be comparing two numbers. If the first number is greater then first number will be compared with the third number whichever number is greater print that. If the first number is smaller then compare second number with the third number, whichever is greater among two print that number.
In this algorithm we declare four variables a, b and c for reading the numbers and largest for storing it. Then read the three variables. Then we use Ternary operator before question mark condition is given. If it is true then the condition before ":" it is taken otherwise after that. a > b? (a > c? a: c): (b > c? b: c). In this first we compare a > b. If a is greater then (a > c? a: c) will be followed otherwise (b > c? b: c). Let us take both the cases where
At last we print the value of Max.
Note: If all the three numbers are equal then it will print zero as this condition is not stated. We have declared the number as int so, this algorithm will be valid up to the range of int.
Our Quiz prepared by Experts Helps you identify your knowledge in Algorithms. Everyone should atleast attempt this Quiz Once.