EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that takes two numbers as input from the user and displays all odd numbers between them using the do-while loop.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of do-while loops, if statements, and modulus operator

📖 Introduction

In this practical, you will:

  • Take two numbers as input from the user
  • Find all odd numbers between the two numbers
  • Use a do-while loop to iterate through the range
  • Use the modulus operator (%) to check if a number is odd

An odd number is not divisible by 2. You can check this using num % 2 != 0.

💡 Tip: The do-while loop is useful when you want to ensure the loop runs at least once, even if the range is empty.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named odd_between_do_while.c.
  3. Write the following code:
/* Practical 48: Program to display odd numbers between two numbers using do-while loop */ #include <stdio.h> int main() { int num1, num2, start, end, count = 0; // Take input from user printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); // Ensure start is the smaller number and end is the larger number if (num1 < num2) { start = num1; end = num2; } else { start = num2; end = num1; } /* ============================================ Using do-while loop to display odd numbers ============================================ */ printf("\n========== ODD NUMBERS ==========\n"); int i = start; // Initialize counter do { if (i % 2 != 0) // Check if the number is odd { printf("%d\n", i); count++; // Count the odd numbers } i++; } while (i <= end); printf("\nTotal odd numbers: %d\n", count); printf("==================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int num1, num2, start, end, count = 0; — Declares variables for input, range, and counter.
  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) — Ensures start is the smaller number and end is the larger number.
  6. do — Starts the do-while loop.
  7. if (i % 2 != 0) — Checks if the current number is odd.
  8. printf("%d\n", i); — Displays the odd number.
  9. count++; — Increments the count of odd numbers.
  10. i++; — Increments the counter.
  11. while (i <= end); — Condition checked after the body.

Step 3: Save, Compile, and Run

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

Run the program:

./odd_between_do_while

Sample Input & Output:

Enter first number: 1 Enter second number: 10 ========== ODD NUMBERS ========== 1 3 5 7 9 Total odd numbers: 5 ==================================
Enter first number: 15 Enter second number: 25 ========== ODD NUMBERS ========== 15 17 19 21 23 25 Total odd numbers: 6 ==================================
Enter first number: 10 Enter second number: 20 ========== ODD NUMBERS ========== 11 13 15 17 19 Total odd numbers: 5 ==================================

Step 4: Test with Different Inputs

  1. Test 1: 1, 10 → Odd numbers: 1, 3, 5, 7, 9 (Count: 5)
  2. Test 2: 5, 15 → Odd numbers: 5, 7, 9, 11, 13, 15 (Count: 6)
  3. Test 3: 10, 20 → Odd numbers: 11, 13, 15, 17, 19 (Count: 5)
  4. Test 4: -5, 5 → Odd numbers: -5, -3, -1, 1, 3, 5 (Count: 6)
  5. Test 5: 0, 0 → No odd numbers (Count: 0)

📊 Do-While Loop Execution (Step-by-Step)

Iteration i Value Condition i <= end Is i odd? Output i++
1 1 ✅ True ✅ Yes 1 2
2 2 ✅ True ❌ No - 3
3 3 ✅ True ✅ Yes 3 4
4 4 ✅ True ❌ No - 5
5 5 ✅ True ✅ Yes 5 6
6 6 ✅ True ❌ No - 7
7 7 ✅ True ✅ Yes 7 8
8 8 ✅ True ❌ No - 9
9 9 ✅ True ✅ Yes 9 10
10 10 ❌ False - (Loop ends) -

📊 Alternative Methods

/* Alternative 1: Using while loop */ int i = start; while (i <= end) { if (i % 2 != 0) printf("%d\n", i); i++; }
/* Alternative 2: Using for loop */ for (int i = start; i <= end; i++) { if (i % 2 != 0) printf("%d\n", i); }
/* Alternative 3: Start from the first odd number (more efficient) */ int i; // If start is even, start from start + 1 if (start % 2 == 0) i = start + 1; else i = start; while (i <= end) { printf("%d\n", i); i += 2; // Jump to next odd number }

📊 Observations

  • The program successfully takes two numbers as input from the user.
  • It correctly identifies the smaller and larger numbers to define the range.
  • The do-while loop iterates through all numbers in the range.
  • The if statement checks if each number is odd using i % 2 != 0.
  • Only odd numbers are displayed.
  • The program also counts and displays the total number of odd numbers.

✅ Result

You have successfully written a C program that takes two numbers as input and displays all odd numbers between them using the do-while loop. You have learned how to use loops and conditional statements together to solve problems.

📌 Important: The condition i % 2 != 0 checks if a number is odd. The do-while loop ensures the loop runs at least once, which is useful even if the range is empty.
💡 Tip: You can make the program more efficient by starting from the first odd number and incrementing by 2, rather than checking every number with if.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو صارف سے دو اعداد ان پٹ کے طور پر لے اور do-while لوپ کا استعمال کرتے ہوئے ان کے درمیان تمام طاق اعداد ظاہر کرے۔

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

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

📖 تعارف

اس عملی میں، آپ:

  • صارف سے دو اعداد ان پٹ کے طور پر لیں گے
  • دو اعداد کے درمیان تمام طاق اعداد تلاش کریں گے
  • رینج میں تکرار کرنے کے لیے do-while لوپ استعمال کریں گے
  • یہ چیک کرنے کے لیے ماڈیولس آپریٹر (%) استعمال کریں گے کہ آیا عدد طاق ہے

طاق عدد 2 سے تقسیم نہیں ہوتا۔ آپ اسے num % 2 != 0 کے ساتھ چیک کر سکتے ہیں۔

💡 مشورہ: do-while لوپ اس وقت مفید ہے جب آپ یقینی بنانا چاہتے ہیں کہ لوپ کم از کم ایک بار چلے، چاہے رینج خالی ہو۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. odd_between_do_while.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 48: Program to display odd numbers between two numbers using do-while loop */ #include <stdio.h> int main() { int num1, num2, start, end, count = 0; // Take input from user printf("Enter first number: "); scanf("%d", &num1); printf("Enter second number: "); scanf("%d", &num2); // Ensure start is the smaller number and end is the larger number if (num1 < num2) { start = num1; end = num2; } else { start = num2; end = num1; } /* ============================================ Using do-while loop to display odd numbers ============================================ */ printf("\n========== ODD NUMBERS ==========\n"); int i = start; // Initialize counter do { if (i % 2 != 0) // Check if the number is odd { printf("%d\n", i); count++; // Count the odd numbers } i++; } while (i <= end); printf("\nTotal odd numbers: %d\n", count); printf("==================================\n"); return 0; }

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

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. int num1, num2, start, end, count = 0; — ان پٹ، رینج، اور کاؤنٹر کے لیے متغیرات کا اعلان کرتا ہے۔
  3. scanf("%d", &num1); — صارف سے پہلا عدد پڑھتا ہے۔
  4. scanf("%d", &num2); — صارف سے دوسرا عدد پڑھتا ہے۔
  5. if (num1 < num2) — یقینی بناتا ہے کہ start چھوٹا عدد ہے اور end بڑا عدد ہے۔
  6. do — do-while لوپ شروع کرتا ہے۔
  7. if (i % 2 != 0) — چیک کرتا ہے کہ آیا موجودہ عدد طاق ہے۔
  8. printf("%d\n", i); — طاق عدد ظاہر کرتا ہے۔
  9. count++; — طاق اعداد کی تعداد بڑھاتا ہے۔
  10. i++; — کاؤنٹر کو بڑھاتا ہے۔
  11. while (i <= end); — شرط باڈی کے بعد چیک ہوتی ہے۔

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

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

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

./odd_between_do_while

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

Enter first number: 1 Enter second number: 10 ========== ODD NUMBERS ========== 1 3 5 7 9 Total odd numbers: 5 ==================================

📊 do-while لوپ کا مرحلہ وار عمل

تکرار i کی قیمت شرط i <= end کیا i طاق ہے؟ آؤٹ پٹ i++
1 1 ✅ درست ✅ ہاں 1 2
2 2 ✅ درست ❌ نہیں - 3
3 3 ✅ درست ✅ ہاں 3 4
4 4 ✅ درست ❌ نہیں - 5
5 5 ✅ درست ✅ ہاں 5 6
6 6 ✅ درست ❌ نہیں - 7
7 7 ✅ درست ✅ ہاں 7 8
8 8 ✅ درست ❌ نہیں - 9
9 9 ✅ درست ✅ ہاں 9 10
10 10 ❌ غلط - (لوپ ختم) -

📊 مشاہدات

  • پروگرام کامیابی سے صارف سے دو اعداد ان پٹ کے طور پر لیتا ہے۔
  • یہ رینج کی وضاحت کے لیے چھوٹے اور بڑے اعداد کی درست شناخت کرتا ہے۔
  • do-while لوپ رینج میں تمام اعداد پر تکرار کرتا ہے۔
  • if بیان i % 2 != 0 کا استعمال کرتے ہوئے چیک کرتا ہے کہ آیا ہر عدد طاق ہے۔
  • صرف طاق اعداد ظاہر کیے جاتے ہیں۔
  • پروگرام طاق اعداد کی کل تعداد کو بھی شمار اور ظاہر کرتا ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو صارف سے دو اعداد ان پٹ کے طور پر لیتا ہے اور do-while لوپ کا استعمال کرتے ہوئے ان کے درمیان تمام طاق اعداد ظاہر کرتا ہے۔ آپ نے مسائل کو حل کرنے کے لیے لوپ اور مشروط بیانات کو ایک ساتھ استعمال کرنا سیکھ لیا ہے۔

📌 اہم: شرط i % 2 != 0 چیک کرتی ہے کہ آیا کوئی عدد طاق ہے۔ do-while لوپ اس بات کو یقینی بناتا ہے کہ لوپ کم از کم ایک بار چلے، جو رینج خالی ہونے پر بھی مفید ہے۔
💡 مشورہ: آپ پہلے طاق عدد سے شروع کر کے 2 کا اضافہ کرکے پروگرام کو زیادہ موثر بنا سکتے ہیں، بجائے ہر عدد کو if کے ساتھ چیک کرنے کے۔
⬅ Back to Practical Notebooks