EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that takes three numbers as input from the user and prints the largest among them using the if-else structure.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of if-else statements and comparison operators

📖 Introduction

In this practical, you will compare three numbers entered by the user and determine which one is the largest. This is a very common programming problem that helps you understand nested conditions and logical thinking.

The logic for finding the largest of three numbers is:

  • If num1 > num2 && num1 > num3, then num1 is the largest.
  • If num2 > num1 && num2 > num3, then num2 is the largest.
  • Otherwise, num3 is the largest.

You will use:

  • Comparison operators (>, ==)
  • Logical AND (&&) operator to combine conditions
  • if-else if-else statements
💡 Tip: The logical AND (&&) operator checks if both conditions are true. For example, num1 > num2 && num1 > num3 means "num1 is greater than num2 AND num1 is greater than num3".

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named largest_three.c.
  3. Write the following code:
/* Practical 26: Program to print the largest of three numbers */ #include <stdio.h> int main() { // Declare variables to store three numbers int num1, num2, num3; // Take input from user printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); printf("Enter third number: "); scanf("%d", &num3); // Display the numbers entered printf("\n========== COMPARISON ==========\n"); printf("First Number : %d\n", num1); printf("Second Number : %d\n", num2); printf("Third Number : %d\n", num3); /* ============================================ Using if-else structure to find largest number ============================================ */ if (num1 > num2 && num1 > num3) { printf("\n✅ %d is the LARGEST number.\n", num1); } else if (num2 > num1 && num2 > num3) { printf("\n✅ %d is the LARGEST number.\n", num2); } else { printf("\n✅ %d is the LARGEST number.\n", num3); } printf("==================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int num1, num2, num3; — Declares three integer variables.
  3. scanf() — Reads three numbers from the user.
  4. if (num1 > num2 && num1 > num3) — Checks if num1 is greater than both num2 and num3.
  5. else if (num2 > num1 && num2 > num3) — Checks if num2 is greater than both num1 and num3.
  6. else — If both conditions are false, num3 is the largest.

Step 3: Save, Compile, and Run

  1. Save the file (Ctrl + S).
  2. Open the terminal and compile using the command:
gcc largest_three.c -o largest_three

Run the program:

./largest_three

Sample Input & Output:

Enter first number: 25 Enter second number: 18 Enter third number: 30 ========== COMPARISON ========== First Number : 25 Second Number : 18 Third Number : 30 ✅ 30 is the LARGEST number. ==================================
Enter first number: 45 Enter second number: 22 Enter third number: 30 ========== COMPARISON ========== First Number : 45 Second Number : 22 Third Number : 30 ✅ 45 is the LARGEST number. ==================================
Enter first number: 12 Enter second number: 35 Enter third number: 20 ========== COMPARISON ========== First Number : 12 Second Number : 35 Third Number : 20 ✅ 35 is the LARGEST number. ==================================

Step 4: Test with Different Inputs

  1. Test 1: 10, 20, 15 → Largest is 20
  2. Test 2: 30, 25, 22 → Largest is 30
  3. Test 3: 12, 8, 9 → Largest is 12
  4. Test 4: -5, 0, 5 → Largest is 5
  5. Test 5: -10, -20, -5 → Largest is -5 (less negative)
  6. Test 6: 7, 7, 5 → Largest is 7

📊 Comparison Operators and Logical Operators Used

Operator Type Meaning Example
> Comparison Greater than num1 > num2
&& Logical AND Both conditions must be true num1 > num2 && num1 > num3
== Comparison Equal to num1 == num2

📊 Alternative Methods

/* Alternative 1: Using nested if-else */ int largest = num1; if (num2 > largest) largest = num2; if (num3 > largest) largest = num3; printf("Largest is: %d", largest);
/* Alternative 2: Using ternary operator (nested) */ int largest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3); printf("Largest is: %d", largest);
/* Alternative 3: Using a function */ int max(int a, int b, int c) { if (a >= b && a >= c) return a; else if (b >= a && b >= c) return b; else return c; }

📊 Observations

  • The program successfully takes three numbers as input from the user.
  • It compares the numbers using the if-else structure.
  • The logical AND (&&) operator is used to check if one number is greater than both others.
  • It correctly identifies the largest number.
  • The program works for positive, negative, zero, and equal numbers.

✅ Result

You have successfully written a C program that finds and prints the largest of three numbers entered by the user. You have learned how to use the if-else structure with logical AND (&&) operator to compare three values.

📌 Important: The logical AND (&&) operator is used to combine multiple conditions. The condition num1 > num2 && num1 > num3 checks if num1 is greater than both num2 and num3.
💡 Tip: You can extend this program to find the largest of four or more numbers by adding more comparisons using else if or by using a loop.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو صارف سے تین اعداد ان پٹ کے طور پر لے اور if-else ڈھانچے کا استعمال کرتے ہوئے ان میں سے سب سے بڑا عدد پرنٹ کرے۔

🛠️ آپ کو کیا چاہیے

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • if-else بیانات اور موازنہ آپریٹرز کی معلومات

📖 تعارف

اس عملی میں، آپ صارف کے درج کردہ تین اعداد کا موازنہ کریں گے اور یہ تعین کریں گے کہ ان میں سے سب سے بڑا کون سا ہے۔ یہ ایک بہت عام پروگرامنگ مسئلہ ہے جو آپ کو نested شرائط اور منطقی سوچ کو سمجھنے میں مدد کرتا ہے۔

تین اعداد میں سب سے بڑا عدد تلاش کرنے کی منطق یہ ہے:

  • اگر num1 > num2 && num1 > num3، تو num1 سب سے بڑا ہے۔
  • اگر num2 > num1 && num2 > num3، تو num2 سب سے بڑا ہے۔
  • ورنہ، num3 سب سے بڑا ہے۔

آپ استعمال کریں گے:

  • موازنہ آپریٹرز (>، ==)
  • شرائط کو یکجا کرنے کے لیے منطقی AND (&&) آپریٹر
  • if-else if-else بیانات
💡 مشورہ: منطقی AND (&&) آپریٹر چیک کرتا ہے کہ آیا دونوں شرائط درست ہیں۔ مثال کے طور پر، num1 > num2 && num1 > num3 کا مطلب ہے "num1 num2 سے بڑا ہے اور num1 num3 سے بڑا ہے"۔

📝 اقدامات

پہلا قدم: پروگرام لکھیں

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. largest_three.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 26: Program to print the largest of three numbers */ #include <stdio.h> int main() { // Declare variables to store three numbers int num1, num2, num3; // Take input from user printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); printf("Enter third number: "); scanf("%d", &num3); // Display the numbers entered printf("\n========== COMPARISON ==========\n"); printf("First Number : %d\n", num1); printf("Second Number : %d\n", num2); printf("Third Number : %d\n", num3); /* ============================================ Using if-else structure to find largest number ============================================ */ if (num1 > num2 && num1 > num3) { printf("\n✅ %d is the LARGEST number.\n", num1); } else if (num2 > num1 && num2 > num3) { printf("\n✅ %d is the LARGEST number.\n", num2); } else { printf("\n✅ %d is the LARGEST number.\n", num3); } printf("==================================\n"); return 0; }

دوسرا قدم: کوڈ کی وضاحت

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. int num1, num2, num3; — تین انٹیجر متغیرات کا اعلان کرتا ہے۔
  3. scanf() — صارف سے تین اعداد پڑھتا ہے۔
  4. if (num1 > num2 && num1 > num3) — چیک کرتا ہے کہ کیا num1 num2 اور num3 دونوں سے بڑا ہے۔
  5. else if (num2 > num1 && num2 > num3) — چیک کرتا ہے کہ کیا num2 num1 اور num3 دونوں سے بڑا ہے۔
  6. else — اگر دونوں شرائط غلط ہیں تو num3 سب سے بڑا ہے۔

تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں

  1. فائل کو محفوظ کریں (Ctrl + S
  2. ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc largest_three.c -o largest_three

پروگرام چلائیں:

./largest_three

نمونہ ان پٹ اور آؤٹ پٹ:

Enter first number: 25 Enter second number: 18 Enter third number: 30 ========== COMPARISON ========== First Number : 25 Second Number : 18 Third Number : 30 ✅ 30 is the LARGEST number. ==================================

📊 موازنہ آپریٹرز اور منطقی آپریٹرز کا استعمال

آپریٹر قسم معنی مثال
> موازنہ بڑا ہے num1 > num2
&& منطقی AND دونوں شرائط درست ہونی چاہئیں num1 > num2 && num1 > num3
== موازنہ برابر ہے num1 == num2

📊 مشاہدات

  • پروگرام کامیابی سے صارف سے تین اعداد ان پٹ کے طور پر لیتا ہے۔
  • یہ if-else ڈھانچے کا استعمال کرتے ہوئے اعداد کا موازنہ کرتا ہے۔
  • منطقی AND (&&) آپریٹر کا استعمال یہ چیک کرنے کے لیے کیا جاتا ہے کہ آیا ایک عدد دوسرے دونوں اعداد سے بڑا ہے۔
  • یہ سب سے بڑے عدد کی درست شناخت کرتا ہے۔
  • پروگرام مثبت، منفی، صفر اور مساوی اعداد کے لیے کام کرتا ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو صارف کے درج کردہ تین اعداد میں سے سب سے بڑا عدد تلاش کرتا ہے اور پرنٹ کرتا ہے۔ آپ نے if-else ڈھانچے کو منطقی AND (&&) آپریٹر کے ساتھ استعمال کرتے ہوئے تین اقدار کا موازنہ کرنا سیکھ لیا ہے۔

📌 اہم: منطقی AND (&&) آپریٹر کا استعمال متعدد شرائط کو یکجا کرنے کے لیے کیا جاتا ہے۔ شرط num1 > num2 && num1 > num3 چیک کرتی ہے کہ کیا num1 num2 اور num3 دونوں سے بڑا ہے۔
💡 مشورہ: آپ else if کا استعمال کرتے ہوئے مزید موازنے شامل کرکے یا لوپ کا استعمال کرکے اس پروگرام کو چار یا زیادہ اعداد میں سب سے بڑا عدد تلاش کرنے کے لیے بڑھا سکتے ہیں۔
⬅ Back to Practical Notebooks