C program to Find the Largest Number Among Three Numbers
Last Updated : 01 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

C Program to Find the Largest of Three Numbers Using if-else Statement
C // C program to find the largest number among three number // using nested if-else #include <stdio.h> int main() { int c = 10, b = 22, a = 9; // Finding largest by comparing using relational operators if (a >= b) { if (a >= c) printf("%d is the largest number.", a); else printf("%d is the largest number.", c); } else { if (b >= c) printf("%d is the largest number.", b); else printf("%d is the largest number.", c); } return 0; }
OutputThe 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.
C // C program to find the maximum number out of the three // given numbers using if-else statement #include <stdio.h> int main() { int a = 11, b = 2, c = 9; // Finding max using compound expressions if (a >= b && a >= c) printf("%d is the largest number.", a); else if (b >= a && b >= c) printf("%d is the largest number.", b); else printf("%d is the largest number.", c); return 0; }
OutputThe 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.
C // C Program to Find the Largest Number Among Three using // Temporary Variable #include <stdio.h> int main() { int a = 10, b = 22, c = 9; // Assume a is the largest int max = a; // If b is larger than max if (max < b) max = b; // If c is larger than max if (max < c) max = c; printf("%d is the largest number.", max); return 0; }
OutputThe 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.
C // C Program to Find the Largest Number Among Three Numbers // using a Custom Max Function #include <stdio.h> // Custom function to find the maximum of two numbers int findMax(int b, int a) { // Return a if a is greater than or equal to b, else // return b return (a >= b) ? a : b; } int main() { int a = 10, b = 22, c = 9; // Calling max function for the first two numbers and then // for its returned value and third number. int max = findMax(max(a, b), c); printf("%d is the largest number.", max); return 0; }
OutputThe numbers A, B and C are: 10, 22, 9 The largest number is 22
Time complexity: O(1)
Auxiliary space: O(1)
Similar Reads
C Program For Finding A Triplet From Three Linked Lists With Sum Equal To A Given Number Given three linked lists, say a, b and c, find one node from each list such that the sum of the values of the nodes is equal to a given number. For example, if the three linked lists are 12->6->29, 23->5->8, and 90->20->59, and the given number is 101, the output should be triple "
4 min read
C Program to Find Largest Element in an Array In this article, we will learn how to find the largest element in the array using a C program.The simplest method to find the largest element in the array is by iterating the array and comparing each element with the assumed maximum and updating it when the element is greater.C#include <stdio.h
3 min read
How to Find Maximum Value in an Array in C? In C, arrays are data structures that allow the user to store a collection of data of the same type. In this article, we will learn how we can find the maximum value in an array in C. Example Input: arr = {5,3,1,2,4} Output: The maximum value of the array is: 5Finding Maximum Value in an Array in CW
2 min read
Command line arguments example in C Prerequisite: Command_line_argument. The problem is to find the largest integer among the three using command line arguments. Notes: Command-line arguments are given after the name of the program in the command-line shell of Operating Systems. To pass command line arguments, we typically define main
3 min read
C++ Program to Find Largest Among Three Numbers Given three numbers, the task is to find the largest number among them in C++.ExamplesInput: a = 1, b = 2, c = 11Output: 11Explanation: Since 11 is the largest number among all numbers.Input: a = 9, b = 7, c = 5Output: 9Explanation: Since 9 is the largest number among all numbers.Find Largest Among
4 min read