EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that takes three integers as input from the user and checks whether all three are equal or not equal using the if-else structure and logical operators.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of if-else statements, logical AND (&&), and comparison operators

📖 Introduction

In this practical, you will compare three numbers entered by the user and determine if they are all equal or not equal. This extends the concept of comparing two numbers to three numbers.

The logic for checking equality of three numbers is:

  • If num1 == num2 && num2 == num3, then all three are equal.
  • Otherwise, they are not all equal.

You will use:

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

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named equal_three.c.
  3. Write the following code:
/* Practical 28: Program to check if three numbers are equal or not */ #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 check equality of all three numbers ============================================ */ if (num1 == num2 && num2 == num3) { printf("\n✅ All three numbers are EQUAL.\n"); printf("📌 %d == %d == %d\n", num1, num2, num3); } else { printf("\n❌ All three numbers are NOT EQUAL.\n"); printf("📌 %d, %d and %d are different\n", num1, num2, 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 && num2 == num3) — Checks if all three numbers are equal using logical AND.
  5. else — Executes if the numbers are not all equal.

Step 3: Save, Compile, and Run

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

Run the program:

./equal_three

Sample Input & Output:

Enter first number: 25 Enter second number: 25 Enter third number: 25 ========== COMPARISON ========== First Number : 25 Second Number : 25 Third Number : 25 ✅ All three numbers are EQUAL. 📌 25 == 25 == 25 ==================================
Enter first number: 25 Enter second number: 18 Enter third number: 30 ========== COMPARISON ========== First Number : 25 Second Number : 18 Third Number : 30 ❌ All three numbers are NOT EQUAL. 📌 25, 18 and 30 are different ==================================
Enter first number: 25 Enter second number: 25 Enter third number: 30 ========== COMPARISON ========== First Number : 25 Second Number : 25 Third Number : 30 ❌ All three numbers are NOT EQUAL. 📌 25, 25 and 30 are different ==================================

Step 4: Test with Different Inputs

  1. Test 1: 10, 10, 10 → All Equal
  2. Test 2: 10, 20, 10 → Not All Equal
  3. Test 3: 10, 20, 30 → Not All Equal
  4. Test 4: -5, -5, -5 → All Equal
  5. Test 5: -5, 5, -5 → Not All Equal
  6. Test 6: 0, 0, 0 → All Equal
  7. Test 7: 0, 1, 0 → Not All Equal

📊 Comparison and Logical Operators Used

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

📊 Possible Cases

Condition Result Example
num1 == num2 && num2 == num3 ✅ All Equal 5, 5, 5
num1 == num2 && num2 != num3 ❌ Not All Equal 5, 5, 3
num1 != num2 && num2 == num3 ❌ Not All Equal 5, 3, 3
num1 == num3 && num2 != num3 ❌ Not All Equal 5, 3, 5
num1 != num2 && num2 != num3 ❌ Not All Equal 5, 3, 7

📊 Alternative Methods

/* Alternative 1: Using nested if-else */ if (num1 == num2) { if (num2 == num3) printf("All Equal"); else printf("Not Equal"); } else { printf("Not Equal"); }
/* Alternative 2: Using multiple if statements */ if (num1 == num2 && num2 == num3) printf("All Equal"); else printf("Not Equal");

📊 Observations

  • The program successfully takes three integers as input from the user.
  • It compares all three numbers using the == operator.
  • The logical AND (&&) operator is used to combine two equality conditions.
  • If all three numbers are equal, the program displays "All Equal".
  • If any of the numbers is different, the program displays "Not Equal".
  • The program works for positive, negative, and zero values.

✅ Result

You have successfully written a C program that checks whether three numbers entered by the user are all equal or not. You have learned how to use the logical AND (&&) operator to combine multiple conditions in an if statement.

📌 Important: The condition num1 == num2 && num2 == num3 checks if both pairs are equal. If both are true, then all three numbers are equal.
💡 Tip: You can also write num1 == num2 && num1 == num3 to check equality. Both num1 == num2 && num2 == num3 and num1 == num2 && num1 == num3 work correctly.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو صارف سے تین انٹیجرز ان پٹ کے طور پر لے اور چیک کرے کہ آیا تینوں برابر ہیں یا نہیں۔

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

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

📖 تعارف

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

تین اعداد کی برابری چیک کرنے کی منطق یہ ہے:

  • اگر num1 == num2 && num2 == num3، تو تینوں برابر ہیں۔
  • ورنہ، وہ سب برابر نہیں ہیں۔

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

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

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. equal_three.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 28: Program to check if three numbers are equal or not */ #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 check equality of all three numbers ============================================ */ if (num1 == num2 && num2 == num3) { printf("\n✅ All three numbers are EQUAL.\n"); printf("📌 %d == %d == %d\n", num1, num2, num3); } else { printf("\n❌ All three numbers are NOT EQUAL.\n"); printf("📌 %d, %d and %d are different\n", num1, num2, num3); } printf("==================================\n"); return 0; }

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

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

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

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

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

./equal_three

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

Enter first number: 25 Enter second number: 25 Enter third number: 25 ========== COMPARISON ========== First Number : 25 Second Number : 25 Third Number : 25 ✅ All three numbers are EQUAL. 📌 25 == 25 == 25 ==================================
Enter first number: 25 Enter second number: 18 Enter third number: 30 ========== COMPARISON ========== First Number : 25 Second Number : 18 Third Number : 30 ❌ All three numbers are NOT EQUAL. 📌 25, 18 and 30 are different ==================================

📊 ممکنہ صورتیں

شرط نتیجہ مثال
num1 == num2 && num2 == num3 ✅ سب برابر 5, 5, 5
num1 == num2 && num2 != num3 ❌ سب برابر نہیں 5, 5, 3
num1 != num2 && num2 == num3 ❌ سب برابر نہیں 5, 3, 3
num1 == num3 && num2 != num3 ❌ سب برابر نہیں 5, 3, 5
num1 != num2 && num2 != num3 ❌ سب برابر نہیں 5, 3, 7

📊 مشاہدات

  • پروگرام کامیابی سے صارف سے تین انٹیجرز ان پٹ کے طور پر لیتا ہے۔
  • یہ == آپریٹر کا استعمال کرتے ہوئے تینوں اعداد کا موازنہ کرتا ہے۔
  • منطقی AND (&&) آپریٹر کا استعمال دو برابری کی شرائط کو یکجا کرنے کے لیے کیا جاتا ہے۔
  • اگر تینوں اعداد برابر ہیں تو پروگرام "All Equal" ظاہر کرتا ہے۔
  • اگر کوئی بھی عدد مختلف ہے تو پروگرام "Not Equal" ظاہر کرتا ہے۔
  • پروگرام مثبت، منفی اور صفر اقدار کے لیے کام کرتا ہے۔

✅ نتیجہ

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

📌 اہم: شرط num1 == num2 && num2 == num3 چیک کرتی ہے کہ آیا دونوں جوڑے برابر ہیں۔ اگر دونوں درست ہیں تو تینوں اعداد برابر ہیں۔
💡 مشورہ: آپ برابری چیک کرنے کے لیے num1 == num2 && num1 == num3 بھی لکھ سکتے ہیں۔ num1 == num2 && num2 == num3 اور num1 == num2 && num1 == num3 دونوں صحیح کام کرتے ہیں۔
⬅ Back to Practical Notebooks