VOOZH about

URL: https://www.geeksforgeeks.org/c/c-program-to-find-the-largest-number-among-three-numbers/

⇱ C program to Find the Largest Number Among Three Numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C program to Find the Largest Number Among Three Numbers

Last Updated : 1 May, 2025

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.

Finding the Largest Number Among Three Numbers in C

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:

  • Check if a is greater than b and a is also greater than c. If both conditions are true, a is the largest.
  • Otherwise, c is the largest.
  • If a is not greater than b and b is greater than or equal to c, b is the largest.
  • Otherwise, c is the largest.

Flowchart of the Logic

👁 largest-among-three-numbers

C Program to Find the Largest of Three Numbers Using if-else Statement


Output
The numbers A, B and C are: 10, 22, 9
22 is the largest number.

Time complexity: O(1)
Auxiliary space: O(1)

Other Different Methods to Find the Largest of Three Numbers

There are many different ways using which we can find the largest number between three numbers:

1. Using Compound Expression in if-else

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.


Output
The numbers A, B and C are: 10, 22, 9
22 is the largest number.

Time complexity: O(1)
Auxiliary space: O(1)

2. Using Temporary Variable

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.


Output
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)

3. Using a Custom Max Function

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.


Output
The numbers A, B and C are: 10, 22, 9
The largest number is 22

Time complexity: O(1)
Auxiliary space: O(1)

Comment
Article Tags: