EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that takes a number as input from the user and checks whether it is an even or odd number using the modulus operator (%) and logical operators.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of modulus operator and conditional statements

📖 Introduction

In mathematics, numbers are classified as even or odd:

  • Even numbers: Divisible by 2 without a remainder (e.g., 2, 4, 6, 8, 10)
  • Odd numbers: Not divisible by 2 (e.g., 1, 3, 5, 7, 9)

In C/C++, you can check if a number is even or odd using the modulus operator (%):

  • If number % 2 == 0, the number is even
  • If number % 2 != 0, the number is odd

In this practical, you will also use the logical OR operator (||) to check multiple conditions, even though it's not strictly necessary for this simple case. This will help you understand how to combine conditions.

💡 Tip: The modulus operator % gives the remainder after division. For example, 5 % 2 = 1 (odd), 4 % 2 = 0 (even).

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named even_odd.c.
  3. Write the following code:
/* Practical 21: Program to check even or odd number using logical OR */ #include <stdio.h> int main() { // Declare a variable to store the number int num; // Take input from user printf("Enter a number: "); scanf("%d", &num); // Display the number entered printf("\n========== NUMBER CHECK ==========\n"); printf("You entered: %d\n", num); // Check if the number is even using modulus operator // Using logical OR to demonstrate its usage (though not needed here) if (num % 2 == 0) { printf("✅ %d is an EVEN number.\n", num); } else { printf("✅ %d is an ODD number.\n", num); } // Additional check using logical OR (demonstration) // Checking if number is even by checking divisibility by 2 OR 4 if (num % 2 == 0 || num % 4 == 0) { printf("📌 (Using OR) %d is divisible by 2 OR 4.\n", num); } printf("====================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int num; — Declares an integer variable.
  3. scanf("%d", &num); — Reads the number from the user.
  4. num % 2 == 0 — Checks if the number is divisible by 2 (even).
  5. if-else — Displays whether the number is even or odd.
  6. num % 2 == 0 || num % 4 == 0 — Demonstrates the use of logical OR operator.

Step 3: Save, Compile, and Run

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

Run the program:

./even_odd

Sample Input & Output:

Enter a number: 10 ========== NUMBER CHECK ========== You entered: 10 ✅ 10 is an EVEN number. 📌 (Using OR) 10 is divisible by 2 OR 4. ====================================
Enter a number: 7 ========== NUMBER CHECK ========== You entered: 7 ✅ 7 is an ODD number. ====================================

Step 4: Test with Different Inputs

  1. Test 1: 2 → Even
  2. Test 2: 3 → Odd
  3. Test 3: 0 → Even (0 is divisible by 2)
  4. Test 4: -4 → Even (negative even)
  5. Test 5: -5 → Odd (negative odd)
  6. Test 6: 8 → Even

📊 Even and Odd Numbers

Type Definition Examples
Even Divisible by 2 (remainder = 0) 2, 4, 6, 8, 10, 12, 0, -2, -4
Odd Not divisible by 2 (remainder = 1) 1, 3, 5, 7, 9, 11, -1, -3, -5

📊 Logical Operators Used

Operator Name Meaning Example
|| OR At least one condition is true num % 2 == 0 || num % 4 == 0
&& AND Both conditions must be true num > 0 && num % 2 == 0

📊 Alternative Method (Using Ternary Operator)

/* Alternative: Using ternary operator */ (num % 2 == 0) ? printf("%d is Even", num) : printf("%d is Odd", num);

📊 Observations

  • The program successfully takes a number as input from the user.
  • The modulus operator % is used to check if the number is divisible by 2.
  • If num % 2 == 0, the number is even; otherwise, it is odd.
  • The logical OR operator || was demonstrated to show how multiple conditions can be combined.
  • The program works for positive, negative, and zero values.

✅ Result

You have successfully written a C program that checks whether a number is even or odd using the modulus operator (%). You have also learned how to use the logical OR operator (||) to combine conditions in a program.

📌 Important: The modulus operator % gives the remainder. num % 2 gives 0 for even numbers and 1 for odd numbers.
💡 Tip: You can use the logical OR operator to check if a number satisfies multiple conditions. For example, (num % 2 == 0 || num % 3 == 0) checks if the number is divisible by 2 OR 3.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو صارف سے ایک عدد ان پٹ کے طور پر لے اور ماڈیولس آپریٹر (%) اور منطقی آپریٹرز کا استعمال کرتے ہوئے چیک کرے کہ آیا یہ جفت ہے یا طاق۔

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

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • ماڈیولس آپریٹر اور مشروط بیانات کی معلومات

📖 تعارف

ریاضی میں، اعداد کو جفت اور طاق میں تقسیم کیا جاتا ہے:

  • جفت (Even) اعداد: وہ اعداد جو 2 سے مکمل طور پر تقسیم ہوں (مثال: 2، 4، 6، 8، 10)
  • طاق (Odd) اعداد: وہ اعداد جو 2 سے مکمل طور پر تقسیم نہ ہوں (مثال: 1، 3، 5، 7، 9)

C/C++ میں، آپ ماڈیولس آپریٹر (%) کا استعمال کرتے ہوئے چیک کر سکتے ہیں کہ آیا کوئی عدد جفت ہے یا طاق:

  • اگر number % 2 == 0، تو عدد جفت ہے
  • اگر number % 2 != 0، تو عدد طاق ہے

اس عملی میں، آپ منطقی OR آپریٹر (||) کا بھی استعمال کریں گے تاکہ متعدد شرائط کو یکجا کرنے کا طریقہ سیکھ سکیں، حالانکہ اس سادہ صورت میں اس کی ضرورت نہیں ہے۔

💡 مشورہ: ماڈیولس آپریٹر % تقسیم کے بعد بقیہ دیتا ہے۔ مثال کے طور پر، 5 % 2 = 1 (طاق)، 4 % 2 = 0 (جفت)۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. even_odd.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 21: Program to check even or odd number using logical OR */ #include <stdio.h> int main() { // Declare a variable to store the number int num; // Take input from user printf("Enter a number: "); scanf("%d", &num); // Display the number entered printf("\n========== NUMBER CHECK ==========\n"); printf("You entered: %d\n", num); // Check if the number is even using modulus operator // Using logical OR to demonstrate its usage (though not needed here) if (num % 2 == 0) { printf("✅ %d is an EVEN number.\n", num); } else { printf("✅ %d is an ODD number.\n", num); } // Additional check using logical OR (demonstration) // Checking if number is even by checking divisibility by 2 OR 4 if (num % 2 == 0 || num % 4 == 0) { printf("📌 (Using OR) %d is divisible by 2 OR 4.\n", num); } printf("====================================\n"); return 0; }

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

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

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

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

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

./even_odd

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

Enter a number: 10 ========== NUMBER CHECK ========== You entered: 10 ✅ 10 is an EVEN number. 📌 (Using OR) 10 is divisible by 2 OR 4. ====================================

📊 جفت اور طاق اعداد

قسم تعریف مثالیں
جفت (Even) 2 سے تقسیم ہو (بقیہ = 0) 2، 4، 6، 8، 10، 12، 0، -2، -4
طاق (Odd) 2 سے تقسیم نہ ہو (بقیہ = 1) 1، 3، 5، 7، 9، 11، -1، -3، -5

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

آپریٹر نام معنی مثال
|| OR کم از کم ایک شرط درست ہے num % 2 == 0 || num % 4 == 0
&& AND دونوں شرائط درست ہونی چاہئیں num > 0 && num % 2 == 0

📊 مشاہدات

  • پروگرام کامیابی سے صارف سے ایک عدد ان پٹ کے طور پر لیتا ہے۔
  • ماڈیولس آپریٹر % کا استعمال یہ چیک کرنے کے لیے کیا جاتا ہے کہ آیا عدد 2 سے تقسیم ہوتا ہے۔
  • اگر num % 2 == 0، تو عدد جفت ہے؛ ورنہ طاق ہے۔
  • منطقی OR آپریٹر || کو یہ ظاہر کرنے کے لیے استعمال کیا گیا کہ متعدد شرائط کو کیسے یکجا کیا جا سکتا ہے۔
  • پروگرام مثبت، منفی اور صفر اقدار کے لیے کام کرتا ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو ماڈیولس آپریٹر (%) کا استعمال کرتے ہوئے چیک کرتا ہے کہ آیا کوئی عدد جفت ہے یا طاق۔ آپ نے منطقی OR آپریٹر (||) کا استعمال کرتے ہوئے شرائط کو یکجا کرنا بھی سیکھ لیا ہے۔

📌 اہم: ماڈیولس آپریٹر % بقیہ دیتا ہے۔ num % 2 جفت اعداد کے لیے 0 اور طاق اعداد کے لیے 1 دیتا ہے۔
💡 مشورہ: آپ منطقی OR آپریٹر کا استعمال کرتے ہوئے چیک کر سکتے ہیں کہ آیا کوئی عدد متعدد شرائط کو پورا کرتا ہے۔ مثال کے طور پر، (num % 2 == 0 || num % 3 == 0) چیک کرتا ہے کہ آیا عدد 2 سے یا 3 سے تقسیم ہوتا ہے۔
⬅ Back to Practical Notebooks