![]() |
VOOZH | about |
Given 3 integer numbers, the task is to find the largest number among them.
Examples
Input: a = 10, b = 22, c = 19
Output: 22 is the largest number.
Explanation: Among the numbers 5, 8, and 3, the largest number is 8.
Input: a = 12, b = 7, c = 9
Output: 12 is the largest number.
Explanation: Among the numbers 12, 7, and 9, the largest number is 12.
The basic method to find the largest number among three numbers is by using an if statement to compare the numbers with each other in pairs. Let's consider three numbers a, b, and c:
The numbers A, B and C are: 10, 22, 9 22 is the largest number.
Time complexity: O(1)
Auxiliary space: O(1)
Table of Content
There are many different ways using which we can find the largest number between three numbers:
We can compare a number with other two number in a single if statement using AND operator. It allows us to combine the two relational expressions from the above program making the program concise.
The numbers A, B and C are: 10, 22, 9 22 is the largest number.
Time complexity: O(1)
Auxiliary space: O(1)
In this method, we assume one of the numbers as maximum and assign it to a temporary variable. We then compare this temporary variable with the other two numbers one by one and if the max is smaller than the compared number, we assign the compared number to the max.
The numbers A, B and C are: 10, 22, 9 Maximum among 10, 22, and 9 is: 22
Time complexity: O(1)
Auxiliary space: O(1)
We can define a max() function that takes two numbers and return the greater one. We can use this function to find the largest among three number by first using max with two numbers and then using it with third number.
The numbers A, B and C are: 10, 22, 9 The largest number is 22
Time complexity: O(1)
Auxiliary space: O(1)