⑯Find Largest of Three Numbers (with complete explanation and output examples)
✅ Find Largest of Three Numbers
🔹 Problem Statement
Given three numbers, find which one is the largest.
🧠 Method 1: Using Simple if-else Conditions (Best Method)
🔸 Logic Explanation
Suppose the numbers are:
-
a -
b -
c
Steps:
-
Compare
awith bothbandc. -
If
ais greater than or equal to both →ais largest. -
Otherwise compare
bwithaandc. -
If
bis greater than or equal to both →bis largest. -
Else →
cis largest.
💻 C Program
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c) {
printf("Largest number is: %d", a);
}
else if (b >= a && b >= c) {
printf("Largest number is: %d", b);
}
else {
printf("Largest number is: %d", c);
}
return 0;
}
🔍 Dry Run Example 1
Input:
a = 10
b = 25
c = 15
Execution:
-
Check:
10 >= 25 && 10 >= 15❌ False -
Check:
25 >= 10 && 25 >= 15✅ True
Output:
Largest number is: 25
🧠 Method 2: Using Nested if
if(a > b) {
if(a > c)
printf("Largest is %d", a);
else
printf("Largest is %d", c);
}
else {
if(b > c)
printf("Largest is %d", b);
else
printf("Largest is %d", c);
}
🧠 Method 3: Using Ternary Operator (Short Method)
int largest = (a > b) ?
((a > c) ? a : c) :
((b > c) ? b : c);
⚡ Time Complexity
-
Only comparisons
-
Time Complexity = O(1) (constant time)
🎯 Important Points for Exams
✔ Use >= to handle equal values
✔ Always check all conditions carefully
✔ Works for negative numbers also
✅ Conclusion
This program efficiently calculates how to find largest of three numbers. It is beginner-friendly and commonly asked in C programming interviews and exams.
Comments
Post a Comment