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 even or odd using the if-else structure.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of if-else statements and the modulus operator

📖 Introduction

The if-else statement is one of the most fundamental decision-making structures in C/C++. It allows you to execute different blocks of code based on whether a condition is true or false.

In this practical, you will use the if-else structure to check if a number is even or odd:

  • An even number is divisible by 2 (remainder is 0).
  • An odd number is not divisible by 2 (remainder is 1).

You will use the modulus operator (%) to find the remainder when the number is divided by 2.

💡 Tip: The if-else structure is perfect for this type of binary decision (even or odd). The condition num % 2 == 0 checks if the number is even.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named even_odd_ifelse.c.
  3. Write the following code:
/* Practical 24: Program to check even or odd using if-else */ #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); /* ============================================ Using if-else structure to check even or odd ============================================ */ if (num % 2 == 0) { printf("✅ %d is an EVEN number.\n", num); printf("📌 (Even numbers are divisible by 2)\n"); } else { printf("✅ %d is an ODD number.\n", num); printf("📌 (Odd numbers are NOT divisible by 2)\n"); } 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 to store the number.
  3. scanf("%d", &num); — Reads the number from the user.
  4. if (num % 2 == 0) — Checks if the number is divisible by 2 (even).
  5. else — Executes if the condition is false (odd).
  6. printf() — Displays the result with appropriate messages.

Step 3: Save, Compile, and Run

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

Run the program:

./even_odd_ifelse

Sample Input & Output:

Enter a number: 10 ========== NUMBER CHECK ========== You entered: 10 ✅ 10 is an EVEN number. 📌 (Even numbers are divisible by 2) ======================================
Enter a number: 7 ========== NUMBER CHECK ========== You entered: 7 ✅ 7 is an ODD number. 📌 (Odd numbers are NOT divisible by 2) ======================================

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: 100 → Even
  7. Test 7: 99 → Odd

📊 if-else Structure Syntax

Syntax Description
if (condition) Executes the block if condition is true
{ } Block of code to execute
else Executes the block if condition is false
num % 2 == 0 Condition for even number

📊 Even and Odd Numbers Examples

Type Numbers Rule
Even 2, 4, 6, 8, 10, 12, 0, -2, -4, -6 Divisible by 2
Odd 1, 3, 5, 7, 9, 11, -1, -3, -5, -7 Not divisible by 2

📊 Comparison: if-else vs Ternary Operator

Feature if-else ?: (Ternary)
Readability More readable for beginners More compact
Multiple Statements Supports multiple statements Only single expressions
Best Used For Complex decision making Simple assignments
Learning Curve Easier to understand Requires more practice

📊 Observations

  • The if-else structure is easy to read and understand.
  • The modulus operator % is used to find the remainder when the number is divided by 2.
  • If the remainder is 0, the number is even.
  • If the remainder is 1, the number is odd.
  • The program clearly displays the result with explanatory messages.
  • This is the most common and recommended way for beginners to check even/odd numbers.

✅ Result

You have successfully written a C program that uses the if-else structure to check whether a number is even or odd. You have learned how to use the modulus operator and conditional statements to make decisions in your program.

📌 Important: The if-else structure is fundamental to C programming. It allows your program to make decisions based on conditions. The condition num % 2 == 0 checks if the number is even.
💡 Tip: You can extend this program to check if a number is divisible by 3, 4, or any other number by changing the modulus operator and the divisor.

🎯 مقصد

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

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

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

📖 تعارف

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

اس عملی میں، آپ if-else ڈھانچے کا استعمال کرتے ہوئے چیک کریں گے کہ آیا کوئی عدد جفت ہے یا طاق:

  • جفت (Even) عدد 2 سے تقسیم ہوتا ہے (بقیہ 0 ہے)۔
  • طاق (Odd) عدد 2 سے تقسیم نہیں ہوتا (بقیہ 1 ہے)۔

آپ عدد کو 2 سے تقسیم کرنے پر بقیہ معلوم کرنے کے لیے ماڈیولس آپریٹر (%) استعمال کریں گے۔

💡 مشورہ: if-else ڈھانچہ اس قسم کے دو طرفہ فیصلے (جفت یا طاق) کے لیے بہترین ہے۔ شرط num % 2 == 0 چیک کرتی ہے کہ آیا عدد جفت ہے۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. even_odd_ifelse.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 24: Program to check even or odd using if-else */ #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); /* ============================================ Using if-else structure to check even or odd ============================================ */ if (num % 2 == 0) { printf("✅ %d is an EVEN number.\n", num); printf("📌 (Even numbers are divisible by 2)\n"); } else { printf("✅ %d is an ODD number.\n", num); printf("📌 (Odd numbers are NOT divisible by 2)\n"); } printf("======================================\n"); return 0; }

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

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

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

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

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

./even_odd_ifelse

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

Enter a number: 10 ========== NUMBER CHECK ========== You entered: 10 ✅ 10 is an EVEN number. 📌 (Even numbers are divisible by 2) ======================================
Enter a number: 7 ========== NUMBER CHECK ========== You entered: 7 ✅ 7 is an ODD number. 📌 (Odd numbers are NOT divisible by 2) ======================================

📊 if-else ڈھانچے کا نحو

نحو وضاحت
if (condition) اگر شرط درست ہے تو بلاک کو انجام دیتا ہے
{ } کوڈ کا بلاک
else اگر شرط غلط ہے تو بلاک کو انجام دیتا ہے
num % 2 == 0 جفت عدد کی شرط

📊 جفت اور طاق اعداد کی مثالیں

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

📊 مشاہدات

  • if-else ڈھانچہ پڑھنے اور سمجھنے میں آسان ہے۔
  • ماڈیولس آپریٹر % عدد کو 2 سے تقسیم کرنے پر بقیہ معلوم کرنے کے لیے استعمال ہوتا ہے۔
  • اگر بقیہ 0 ہے تو عدد جفت ہے۔
  • اگر بقیہ 1 ہے تو عدد طاق ہے۔
  • پروگرام واضح طور پر نتیجہ کو وضاحتی پیغامات کے ساتھ ظاہر کرتا ہے۔
  • یہ ابتدائیوں کے لیے جفت/طاق چیک کرنے کا سب سے عام اور تجویز کردہ طریقہ ہے۔

✅ نتیجہ

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

📌 اہم: if-else ڈھانچہ C پروگرامنگ میں بنیادی حیثیت رکھتا ہے۔ یہ آپ کے پروگرام کو شرائط کی بنیاد پر فیصلے کرنے کی اجازت دیتا ہے۔ شرط num % 2 == 0 چیک کرتی ہے کہ آیا عدد جفت ہے۔
💡 مشورہ: آپ ماڈیولس آپریٹر اور تقسیم کنندہ کو تبدیل کرکے اس پروگرام کو یہ چیک کرنے کے لیے بڑھا سکتے ہیں کہ آیا کوئی عدد 3، 4، یا کسی دوسرے عدد سے تقسیم ہوتا ہے۔
⬅ Back to Practical Notebooks