⑯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:

  1. Compare a with both b and c.

  2. If a is greater than or equal to both → a is largest.

  3. Otherwise compare b with a and c.

  4. If b is greater than or equal to both → b is largest.

  5. Else → c is 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.


📘 C Programming Series – What’s Next?

👉 In the next post of this series, we will learn Patterns with complete explanation and output examples. Stay tuned! 🚀

Comments

Popular posts from this blog

⓮C Program to calculate Simple Interest (With Program and Explanation)

⑫ C Program to Swap Two Numbers Without Using a Temporary Variable (With Program and Explanation)

9️⃣C Program to Find LCM of Two Numbers (With Program and Explanation)