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 odd or even using the conditional operator (? :), also known as the ternary operator.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of the conditional (ternary) operator

📖 Introduction

The conditional operator (? :) is a shorthand way of writing an if-else statement. Its syntax is:

condition ? expression1 : expression2;
  • If condition is true, expression1 is executed.
  • If condition is false, expression2 is executed.

In this practical, you will use the conditional operator to check if a number is even or odd:

  • Condition: num % 2 == 0
  • If true: "Even"
  • If false: "Odd"
💡 Tip: The conditional operator is also called the ternary operator because it takes three operands: condition, true expression, and false expression.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named odd_even_ternary.c.
  3. Write the following code:
/* Practical 23: Check odd or even using conditional operator */ #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); // Using conditional operator to check even or odd // Syntax: condition ? expression1 : expression2 (num % 2 == 0) ? printf("\n✅ %d is an EVEN number.\n", num) : printf("\n✅ %d is an ODD number.\n", num); // Alternatively, store result in a variable char *result = (num % 2 == 0) ? "Even" : "Odd"; printf("📌 Using string variable: %d is %s.\n", num, result); 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) ? printf(...) : printf(...); — Uses the conditional operator to check if the number is even or odd and displays the result.
  5. char *result = (num % 2 == 0) ? "Even" : "Odd"; — Stores the result in a string variable using the conditional operator.

Step 3: Save, Compile, and Run

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

Run the program:

./odd_even_ternary

Sample Input & Output:

Enter a number: 10 ✅ 10 is an EVEN number. 📌 Using string variable: 10 is Even.
Enter a number: 7 ✅ 7 is an ODD number. 📌 Using string variable: 7 is Odd.

Step 4: Test with Different Inputs

  1. Test 1: 2 → Even
  2. Test 2: 3 → Odd
  3. Test 3: 0 → Even
  4. Test 4: -4 → Even
  5. Test 5: -5 → Odd

📊 Conditional Operator (Ternary Operator) Syntax

Syntax Description Example
condition ? expr1 : expr2 If condition is true, execute expr1; else execute expr2 (num % 2 == 0) ? "Even" : "Odd"

📊 Comparison: if-else vs Ternary Operator

Feature if-else ?: (Ternary)
Syntax Length Longer (multiple lines) Shorter (one line)
Readability More readable for complex logic Better for simple conditions
Multiple Statements Supports multiple statements Only single expressions
Return Value No return value Returns a value
Best Used For Complex decision making Simple assignments

📊 Alternative Method (Using if-else)

/* Alternative: Using if-else (more readable for beginners) */ if (num % 2 == 0) { printf("%d is Even", num); } else { printf("%d is Odd", num); }

📊 Observations

  • The conditional operator ? : provides a compact way to write if-else statements.
  • It is useful for simple conditions where you need to assign a value or execute a single statement.
  • The program successfully checks whether the number is even or odd.
  • Using the conditional operator makes the code shorter and more concise.
  • The result can be stored in a variable for later use.

✅ Result

You have successfully written a C program that checks whether a number is even or odd using the conditional operator (? :). You have learned how to use the ternary operator for compact conditional checks.

📌 Important: The conditional operator works as follows: condition ? value_if_true : value_if_false. It is a concise alternative to if-else.
💡 Tip: The conditional operator is best used for simple conditions. For complex logic with multiple statements, use if-else for better readability.

🎯 مقصد

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

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

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

📖 تعارف

مشروط آپریٹر (? :) if-else بیان لکھنے کا ایک مختصر طریقہ ہے۔ اس کا نحو یہ ہے:

condition ? expression1 : expression2;
  • اگر condition درست ہے تو expression1 پر عمل ہوتا ہے۔
  • اگر condition غلط ہے تو expression2 پر عمل ہوتا ہے۔

اس عملی میں، آپ مشروط آپریٹر کا استعمال کرتے ہوئے چیک کریں گے کہ آیا عدد جفت ہے یا طاق:

  • شرط: num % 2 == 0
  • اگر درست: "Even"
  • اگر غلط: "Odd"
💡 مشورہ: مشروط آپریٹر کو ternary آپریٹر بھی کہا جاتا ہے کیونکہ یہ تین آپرینڈ لیتا ہے: condition، true expression، اور false expression۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. odd_even_ternary.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 23: Check odd or even using conditional operator */ #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); // Using conditional operator to check even or odd // Syntax: condition ? expression1 : expression2 (num % 2 == 0) ? printf("\n✅ %d is an EVEN number.\n", num) : printf("\n✅ %d is an ODD number.\n", num); // Alternatively, store result in a variable char *result = (num % 2 == 0) ? "Even" : "Odd"; printf("📌 Using string variable: %d is %s.\n", num, result); return 0; }

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

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

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

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

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

./odd_even_ternary

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

Enter a number: 10 ✅ 10 is an EVEN number. 📌 Using string variable: 10 is Even.

📊 مشروط آپریٹر (Ternary Operator) کا نحو

نحو وضاحت مثال
condition ? expr1 : expr2 اگر شرط درست ہے تو expr1 پر عمل کریں، ورنہ expr2 پر عمل کریں (num % 2 == 0) ? "Even" : "Odd"

📊 موازنہ: if-else بمقابلہ Ternary Operator

خصوصیت if-else ?: (Ternary)
نحو کی لمبائی لمبا (متعدد لائنیں) چھوٹا (ایک لائن)
پڑھنے میں آسانی پیچیدہ منطق کے لیے بہتر سادہ شرائط کے لیے بہتر
متعدد بیانات متعدد بیانات کو سپورٹ کرتا ہے صرف واحد ایکسپریشنز
واپسی کی قیمت کوئی واپسی کی قیمت نہیں ایک قیمت واپس کرتا ہے
بہترین استعمال پیچیدہ فیصلہ سازی سادہ تفویض

📊 مشاہدات

  • مشروط آپریٹر ? : if-else بیانات لکھنے کا ایک مختصر طریقہ فراہم کرتا ہے۔
  • یہ سادہ شرائط کے لیے مفید ہے جہاں آپ کو کوئی قیمت تفویض کرنی ہو یا ایک بیان پر عمل کرنا ہو۔
  • پروگرام کامیابی سے چیک کرتا ہے کہ آیا عدد جفت ہے یا طاق۔
  • مشروط آپریٹر کا استعمال کوڈ کو چھوٹا اور زیادہ مختصر بناتا ہے۔
  • نتیجہ کو بعد میں استعمال کے لیے متغیر میں محفوظ کیا جا سکتا ہے۔

✅ نتیجہ

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

📌 اہم: مشروط آپریٹر اس طرح کام کرتا ہے: condition ? value_if_true : value_if_false۔ یہ if-else کا ایک مختصر متبادل ہے۔
💡 مشورہ: مشروط آپریٹر سادہ شرائط کے لیے بہترین ہے۔ پیچیدہ منطق کے لیے جس میں متعدد بیانات ہوں، بہتر پڑھنے کی اہلیت کے لیے if-else استعمال کریں۔
⬅ Back to Practical Notebooks