EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that prints multiples of 10 from 10 to 100 (i.e., 10, 20, 30, 40, 50, 60, 70, 80, 90, 100) using the while loop with a step increment.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of while loops and step increments

📖 Introduction

In this practical, you will use a while loop with a step increment of 10 to print multiples of 10 from 10 to 100. This is a common pattern in programming where you need to generate a sequence of numbers with a fixed difference.

The key concept is:

  • Initialize the counter to 10
  • Condition checks if the counter <= 100
  • Increment the counter by 10 each time (i += 10)
💡 Tip: Using a step increment of 10 is more efficient than adding 1 each time and then checking if the number is divisible by 10.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named multiples_of_10.c.
  3. Write the following code:
/* Practical 44: Program to print 10, 20, 30, ..., 100 using while loop */ #include <stdio.h> int main() { int i = 10; // Initialize to 10 /* ============================================ Using while loop with step increment of 10 to print multiples of 10 from 10 to 100 ============================================ */ printf("========== MULTIPLES OF 10 ==========\n"); while (i <= 100) { printf("%d\n", i); i += 10; // Increment by 10 } printf("======================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int i = 10; — Declares and initializes the counter variable to 10.
  3. while (i <= 100) — The while loop runs as long as i is less than or equal to 100.
  4. printf("%d\n", i); — Prints the current value of i.
  5. i += 10; — Increments the counter by 10 after each iteration.

Step 3: Save, Compile, and Run

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

Run the program:

./multiples_of_10

Sample Output:

========== MULTIPLES OF 10 ========== 10 20 30 40 50 60 70 80 90 100 ======================================

Step 4: Modify the Program

  1. Print in one line: Change printf("%d\n", i); to printf("%d ", i);
  2. Print with commas: Add commas between numbers.
  3. Change the step: Change i += 10 to i += 5 to print multiples of 5.
  4. Take user input: Ask the user for the starting value, ending value, and step.
/* Modified: Print in one line with commas */ while (i <= 100) { if (i == 100) printf("%d", i); else printf("%d, ", i); i += 10; } printf("\n");

📊 While Loop Execution (Step-by-Step)

Iteration i Value Condition i <= 100 Output i += 10
1 10 ✅ True 10 20
2 20 ✅ True 20 30
3 30 ✅ True 30 40
4 40 ✅ True 40 50
5 50 ✅ True 50 60
6 60 ✅ True 60 70
7 70 ✅ True 70 80
8 80 ✅ True 80 90
9 90 ✅ True 90 100
10 100 ✅ True 100 110
11 110 ❌ False (Loop ends) -

📊 Multiples of 10

Number Multiple of 10
1 × 1010
2 × 1020
3 × 1030
4 × 1040
5 × 1050
6 × 1060
7 × 1070
8 × 1080
9 × 1090
10 × 10100

📊 Alternative Methods

/* Alternative 1: Using for loop */ for (int i = 10; i <= 100; i += 10) { printf("%d\n", i); }
/* Alternative 2: Using do-while loop */ int i = 10; do { printf("%d\n", i); i += 10; } while (i <= 100);
/* Alternative 3: Using multiplication (1 to 10) */ int i = 1; while (i <= 10) { printf("%d\n", i * 10); i++; }

📊 Observations

  • The while loop with step increment of 10 efficiently prints multiples of 10.
  • The loop runs exactly 10 times (from 10 to 100).
  • The step increment i += 10 is more efficient than adding 1 and checking divisibility.
  • You can easily change the step to print any arithmetic progression.

✅ Result

You have successfully written a C program that prints multiples of 10 from 10 to 100 using the while loop with a step increment. You have learned how to generate arithmetic progressions using loops.

📌 Important: The step increment i += 10 adds 10 to the counter each time. This is more efficient than checking if (i % 10 == 0) inside the loop.
💡 Tip: You can use this pattern to print any arithmetic progression by changing the starting value, ending condition, and step value.

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو 10 سے 100 تک 10 کے ضرب (یعنی 10، 20، 30، 40، 50، 60، 70، 80، 90، 100) کو while لوپ اور مرحلہ وار اضافہ کے ساتھ پرنٹ کرے۔

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

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • while لوپ اور مرحلہ وار اضافہ کی معلومات

📖 تعارف

اس عملی میں، آپ while لوپ اور 10 کے مرحلہ وار اضافہ کا استعمال کرتے ہوئے 10 سے 100 تک 10 کے ضرب پرنٹ کریں گے۔ یہ پروگرامنگ میں ایک عام نمونہ ہے جہاں آپ کو ایک مقررہ فرق کے ساتھ اعداد کا سلسلہ بنانا ہوتا ہے۔

اہم تصور یہ ہے:

  • کاؤنٹر کو 10 سے شروع کریں
  • شرط چیک کرتی ہے کہ کیا کاؤنٹر <= 100 ہے
  • ہر بار کاؤنٹر کو 10 بڑھائیں (i += 10)
💡 مشورہ: 10 کے مرحلہ وار اضافہ کا استعمال ہر بار 1 شامل کرنے اور پھر چیک کرنے سے زیادہ موثر ہے کہ آیا عدد 10 سے تقسیم ہوتا ہے۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. multiples_of_10.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 44: Program to print 10, 20, 30, ..., 100 using while loop */ #include <stdio.h> int main() { int i = 10; // Initialize to 10 /* ============================================ Using while loop with step increment of 10 to print multiples of 10 from 10 to 100 ============================================ */ printf("========== MULTIPLES OF 10 ==========\n"); while (i <= 100) { printf("%d\n", i); i += 10; // Increment by 10 } printf("======================================\n"); return 0; }

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

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. int i = 10; — کاؤنٹر متغیر کا اعلان اور 10 سے شروع کرتا ہے۔
  3. while (i <= 100) — while لوپ اس وقت تک چلتا ہے جب تک i 100 سے کم یا برابر ہے۔
  4. printf("%d\n", i); — i کی موجودہ قیمت پرنٹ کرتا ہے۔
  5. i += 10; — ہر تکرار کے بعد کاؤنٹر کو 10 بڑھاتا ہے۔

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

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

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

./multiples_of_10

نمونہ آؤٹ پٹ:

========== MULTIPLES OF 10 ========== 10 20 30 40 50 60 70 80 90 100 ======================================

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

تکرار i کی قیمت شرط i <= 100 آؤٹ پٹ i += 10
1 10 ✅ درست 10 20
2 20 ✅ درست 20 30
3 30 ✅ درست 30 40
4 40 ✅ درست 40 50
5 50 ✅ درست 50 60
6 60 ✅ درست 60 70
7 70 ✅ درست 70 80
8 80 ✅ درست 80 90
9 90 ✅ درست 90 100
10 100 ✅ درست 100 110
11 110 ❌ غلط (لوپ ختم) -

📊 10 کے ضرب

عدد 10 کا ضرب
1 × 1010
2 × 1020
3 × 1030
4 × 1040
5 × 1050
6 × 1060
7 × 1070
8 × 1080
9 × 1090
10 × 10100

📊 مشاہدات

  • while لوپ 10 کے مرحلہ وار اضافہ کے ساتھ 10 کے ضرب کو مؤثر طریقے سے پرنٹ کرتا ہے۔
  • لوپ بالکل 10 بار چلتا ہے (10 سے 100 تک)۔
  • مرحلہ وار اضافہ i += 10 1 شامل کرنے اور پھر تقسیم پذیری چیک کرنے سے زیادہ موثر ہے۔
  • آپ کسی بھی ریاضیاتی ترقی کو پرنٹ کرنے کے لیے مرحلہ کو آسانی سے تبدیل کر سکتے ہیں۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو while لوپ اور مرحلہ وار اضافہ کے ساتھ 10 سے 100 تک 10 کے ضرب پرنٹ کرتا ہے۔ آپ نے لوپ کا استعمال کرتے ہوئے ریاضیاتی ترقی پیدا کرنا سیکھ لیا ہے۔

📌 اہم: مرحلہ وار اضافہ i += 10 ہر بار کاؤنٹر میں 10 کا اضافہ کرتا ہے۔ یہ لوپ کے اندر if (i % 10 == 0) چیک کرنے سے زیادہ موثر ہے۔
💡 مشورہ: آپ اس نمونے کو ابتدائی قیمت، اختتامی شرط اور مرحلہ کی قیمت کو تبدیل کرکے کسی بھی ریاضیاتی ترقی کو پرنٹ کرنے کے لیے استعمال کر سکتے ہیں۔
⬅ Back to Practical Notebooks