Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA Tutorial
  • Data Structures
  • Algorithms
  • Array
  • Strings
  • Linked List
  • Stack
  • Queue
  • Tree
  • Graph
  • Searching
  • Sorting
  • Recursion
  • Dynamic Programming
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Divide & Conquer
  • Mathematical
  • Geometric
  • Bitwise
  • Greedy
  • Backtracking
  • Branch and Bound
  • Matrix
  • Pattern Searching
  • Randomized
Open In App
Next Article:
C program to Find the Largest Number Among Three Numbers
Next article icon

C program to Find the Largest Number Among Three Numbers

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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; } 

Output
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

  • Other Different Methods to Find the Largest of Three Numbers
    • 1. Using Compound Expression in if-else
    • 2. Using Temporary Variable
    • 3. Using a Custom Max Function

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; } 

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.

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; } 

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.

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; } 

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

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


Next Article
C program to Find the Largest Number Among Three Numbers

R

RishabhPrabhu
Improve
Article Tags :
  • C Programs
  • C Language
  • Computer Science Fundamentals
  • DSA
  • C-Functions
  • C Basic Programs

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences