EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that takes two integers as input from the user and checks whether they are equal or not equal 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 two numbers entered by the user and determine if they are equal or not equal. This is a fundamental programming concept that helps you understand decision-making and comparison operators.

The logic for checking equality is:

  • If num1 == num2, then the numbers are equal.
  • If num1 != num2, then the numbers are not equal.

You will use:

  • Comparison operators (==, !=)
  • if-else statements to compare the numbers
  • printf() to display the result
💡 Tip: The == operator checks if two values are equal, while != checks if they are not equal. Both are very useful in decision-making.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named equal_check.c.
  3. Write the following code:
/* Practical 27: Program to check if two numbers are equal or not */ #include <stdio.h> int main() { // Declare variables to store two numbers int num1, num2; // Take input from user printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); // Display the numbers entered printf("\n========== COMPARISON ==========\n"); printf("First Number : %d\n", num1); printf("Second Number : %d\n", num2); /* ============================================ Using if-else structure to check equality ============================================ */ if (num1 == num2) { printf("\n✅ Both numbers are EQUAL.\n"); printf("📌 %d == %d\n", num1, num2); } else { printf("\n❌ Both numbers are NOT EQUAL.\n"); printf("📌 %d != %d\n", num1, num2); } printf("==================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int num1, num2; — Declares two integer variables.
  3. scanf("%d", &num1); — Reads the first number from the user.
  4. scanf("%d", &num2); — Reads the second number from the user.
  5. if (num1 == num2) — Checks if both numbers are equal.
  6. else — Executes if the numbers are not equal.

Step 3: Save, Compile, and Run

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

Run the program:

./equal_check

Sample Input & Output:

Enter first number: 25 Enter second number: 25 ========== COMPARISON ========== First Number : 25 Second Number : 25 ✅ Both numbers are EQUAL. 📌 25 == 25 ==================================
Enter first number: 25 Enter second number: 18 ========== COMPARISON ========== First Number : 25 Second Number : 18 ❌ Both numbers are NOT EQUAL. 📌 25 != 18 ==================================

Step 4: Test with Different Inputs

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

📊 Comparison Operators Used

Operator Meaning Example Result
== Equal to 5 == 5 True
== Equal to 5 == 3 False
!= Not equal to 5 != 3 True
!= Not equal to 5 != 5 False

📊 Alternative Methods

/* Alternative 1: Using ternary operator */ (num1 == num2) ? printf("Numbers are Equal") : printf("Numbers are Not Equal");
/* Alternative 2: Using nested if-else */ if (num1 == num2) printf("Equal"); else printf("Not Equal");
/* Alternative 3: Using a function */ int isEqual(int a, int b) { if (a == b) return 1; else return 0; }

📊 Observations

  • The program successfully takes two integers as input from the user.
  • It compares the numbers using the == operator.
  • If the numbers are equal, the program displays "EQUAL".
  • If the numbers are not equal, the program displays "NOT EQUAL".
  • The program works for positive, negative, and zero values.
  • The == operator is used for equality checking, while != is used for inequality checking.

✅ Result

You have successfully written a C program that checks whether two numbers entered by the user are equal or not. You have learned how to use the == operator and the if-else structure to compare values.

📌 Important: The == operator checks if two values are equal. It is different from the assignment operator (=) which assigns a value to a variable. == is used for comparison, = is used for assignment.
💡 Tip: You can extend this program to check equality of floating-point numbers, characters, or strings by using appropriate data types and comparison methods.

🎯 مقصد

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

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

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

📖 تعارف

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

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

  • اگر num1 == num2، تو اعداد برابر ہیں۔
  • اگر num1 != num2، تو اعداد برابر نہیں ہیں۔

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

  • موازنہ آپریٹرز (==، !=)
  • اعداد کا موازنہ کرنے کے لیے if-else بیانات
  • نتیجہ ظاہر کرنے کے لیے printf()
💡 مشورہ: == آپریٹر چیک کرتا ہے کہ آیا دو اقدار برابر ہیں، جبکہ != چیک کرتا ہے کہ آیا وہ برابر نہیں ہیں۔ دونوں فیصلہ سازی میں بہت مفید ہیں۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. equal_check.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 27: Program to check if two numbers are equal or not */ #include <stdio.h> int main() { // Declare variables to store two numbers int num1, num2; // Take input from user printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); // Display the numbers entered printf("\n========== COMPARISON ==========\n"); printf("First Number : %d\n", num1); printf("Second Number : %d\n", num2); /* ============================================ Using if-else structure to check equality ============================================ */ if (num1 == num2) { printf("\n✅ Both numbers are EQUAL.\n"); printf("📌 %d == %d\n", num1, num2); } else { printf("\n❌ Both numbers are NOT EQUAL.\n"); printf("📌 %d != %d\n", num1, num2); } printf("==================================\n"); return 0; }

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

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

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

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

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

./equal_check

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

Enter first number: 25 Enter second number: 25 ========== COMPARISON ========== First Number : 25 Second Number : 25 ✅ Both numbers are EQUAL. 📌 25 == 25 ==================================
Enter first number: 25 Enter second number: 18 ========== COMPARISON ========== First Number : 25 Second Number : 18 ❌ Both numbers are NOT EQUAL. 📌 25 != 18 ==================================

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

آپریٹر معنی مثال نتیجہ
== برابر ہے 5 == 5 درست
== برابر ہے 5 == 3 غلط
!= برابر نہیں 5 != 3 درست
!= برابر نہیں 5 != 5 غلط

📊 مشاہدات

  • پروگرام کامیابی سے صارف سے دو انٹیجرز ان پٹ کے طور پر لیتا ہے۔
  • یہ == آپریٹر کا استعمال کرتے ہوئے اعداد کا موازنہ کرتا ہے۔
  • اگر اعداد برابر ہیں تو پروگرام "EQUAL" ظاہر کرتا ہے۔
  • اگر اعداد برابر نہیں ہیں تو پروگرام "NOT EQUAL" ظاہر کرتا ہے۔
  • پروگرام مثبت، منفی اور صفر اقدار کے لیے کام کرتا ہے۔
  • == آپریٹر برابری چیک کرنے کے لیے استعمال ہوتا ہے، جبکہ != عدم برابری چیک کرنے کے لیے استعمال ہوتا ہے۔

✅ نتیجہ

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

📌 اہم: == آپریٹر چیک کرتا ہے کہ آیا دو اقدار برابر ہیں۔ یہ تفویض آپریٹر (=) سے مختلف ہے جو متغیر کو قیمت تفویض کرتا ہے۔ == موازنہ کے لیے استعمال ہوتا ہے، = تفویض کے لیے استعمال ہوتا ہے۔
💡 مشورہ: آپ مناسب ڈیٹا ٹائپس اور موازنہ کے طریقوں کا استعمال کرتے ہوئے اس پروگرام کو فلوٹنگ پوائنٹ اعداد، حروف، یا سٹرنگز کی برابری چیک کرنے کے لیے بڑھا سکتے ہیں۔
⬅ Back to Practical Notebooks