🎯 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
- Open VS Code or any C compiler.
- Create a new file named multiples_of_10.c.
- Write the following code:
#include <stdio.h>
int main()
{
int i = 10;
printf("========== MULTIPLES OF 10 ==========\n");
while (i <= 100)
{
printf("%d\n", i);
i += 10;
}
printf("======================================\n");
return 0;
}
Step 2: Explanation of the Code
- #include <stdio.h> — Includes the standard input/output library.
- int i = 10; — Declares and initializes the counter variable to 10.
- while (i <= 100) — The while loop runs as long as
i is less than or equal to 100.
- printf("%d\n", i); — Prints the current value of
i.
- i += 10; — Increments the counter by 10 after each iteration.
Step 3: Save, Compile, and Run
- Save the file (Ctrl + S).
- Open the terminal and compile using the command:
gcc multiples_of_10.c -o multiples_of_10
./multiples_of_10
Sample Output:
========== MULTIPLES OF 10 ==========
10
20
30
40
50
60
70
80
90
100
======================================
Step 4: Modify the Program
- Print in one line: Change
printf("%d\n", i); to printf("%d ", i);
- Print with commas: Add commas between numbers.
- Change the step: Change
i += 10 to i += 5 to print multiples of 5.
- Take user input: Ask the user for the starting value, ending value, and step.
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 × 10 | 10 |
| 2 × 10 | 20 |
| 3 × 10 | 30 |
| 4 × 10 | 40 |
| 5 × 10 | 50 |
| 6 × 10 | 60 |
| 7 × 10 | 70 |
| 8 × 10 | 80 |
| 9 × 10 | 90 |
| 10 × 10 | 100 |
📊 Alternative Methods
for (int i = 10; i <= 100; i += 10)
{
printf("%d\n", i);
}
int i = 10;
do
{
printf("%d\n", i);
i += 10;
} while (i <= 100);
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 سے تقسیم ہوتا ہے۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- multiples_of_10.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
int main()
{
int i = 10;
printf("========== MULTIPLES OF 10 ==========\n");
while (i <= 100)
{
printf("%d\n", i);
i += 10;
}
printf("======================================\n");
return 0;
}
دوسرا قدم: کوڈ کی وضاحت
- #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
- int i = 10; — کاؤنٹر متغیر کا اعلان اور 10 سے شروع کرتا ہے۔
- while (i <= 100) — while لوپ اس وقت تک چلتا ہے جب تک
i 100 سے کم یا برابر ہے۔
- printf("%d\n", i); —
i کی موجودہ قیمت پرنٹ کرتا ہے۔
- i += 10; — ہر تکرار کے بعد کاؤنٹر کو 10 بڑھاتا ہے۔
تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں
- فائل کو محفوظ کریں (Ctrl + S)۔
- ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
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 × 10 | 10 |
| 2 × 10 | 20 |
| 3 × 10 | 30 |
| 4 × 10 | 40 |
| 5 × 10 | 50 |
| 6 × 10 | 60 |
| 7 × 10 | 70 |
| 8 × 10 | 80 |
| 9 × 10 | 90 |
| 10 × 10 | 100 |
📊 مشاہدات
while لوپ 10 کے مرحلہ وار اضافہ کے ساتھ 10 کے ضرب کو مؤثر طریقے سے پرنٹ کرتا ہے۔
- لوپ بالکل 10 بار چلتا ہے (10 سے 100 تک)۔
- مرحلہ وار اضافہ
i += 10 1 شامل کرنے اور پھر تقسیم پذیری چیک کرنے سے زیادہ موثر ہے۔
- آپ کسی بھی ریاضیاتی ترقی کو پرنٹ کرنے کے لیے مرحلہ کو آسانی سے تبدیل کر سکتے ہیں۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو while لوپ اور مرحلہ وار اضافہ کے ساتھ 10 سے 100 تک 10 کے ضرب پرنٹ کرتا ہے۔ آپ نے لوپ کا استعمال کرتے ہوئے ریاضیاتی ترقی پیدا کرنا سیکھ لیا ہے۔
📌 اہم: مرحلہ وار اضافہ i += 10 ہر بار کاؤنٹر میں 10 کا اضافہ کرتا ہے۔ یہ لوپ کے اندر if (i % 10 == 0) چیک کرنے سے زیادہ موثر ہے۔
💡 مشورہ: آپ اس نمونے کو ابتدائی قیمت، اختتامی شرط اور مرحلہ کی قیمت کو تبدیل کرکے کسی بھی ریاضیاتی ترقی کو پرنٹ کرنے کے لیے استعمال کر سکتے ہیں۔