Program To Find Largest Number Among Three Numbers
Greatest Number Among Three Numbers :
On this page we are going to write program to find the largest number among three numbers.We will understand the problem with help of algorithm and flowchart.Later on this page we will discuss different methods to solve this problem and write the code for the same.
Algorithm to find the largest number among three numbers:
- Start
- Initialize the three numbers let say a,b,c
- Check if a is greater than b.
- If true, then check if a is greater than c.
- If true, then print ‘a’ as the greatest number.
- If false, then print ‘c’ as the greatest number.
- If false, then check if bis greater than num3.
- If true, then print ‘b’ as the greatest number.
- If false, then print ‘c’ as the greatest number
Flowchart:
There are several methods to find largest number among three given number but we are going to write code for three main methods only.
- Largest among three using if-else.
- Largest among three using ternary operator .
- Largest among three using functions.
Method 1:(using if-else)
Run
#include <stdio.h> void main () { int a = 7, b = 5, c = 9; printf ("a = %d\tb = %d\tc = %d\n", a, b, c); 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); }
Output:
a = 7 b = 5 c = 9 9 is the largest number.
Method 2:(Using Ternary Operator)
Run
#include <stdio.h> int main (void) { int a = 7, b = 5, c = 9; printf ("a = %d\tb = %d\tc = %d\n", a, b, c); printf ("Largest of three numbers is %d", a > b ? (a > c ? a : c) : (b > c ? b : c)); return 0; }
Output:
a = 7 b = 5 c = 9 Largest of three numbers is 9
Method 3: (Using Function)
Run
#include <stdio.h> int largest (int a, int b) { if (a > b) return a; return b; } int main (void) { int a = 7, b = 5, c = 9; printf ("a = %d\tb = %d\tc = %d\n", a, b, c); printf ("%d is the largest of all three numbers.\n", largest (largest (a, b), c)); return 0; }
Output:
a = 7 b = 5 c = 9 9 is the largest of all three numbers.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment