🎯 Aim
To write a C/C++ program that prints the multiplication table of 2 from 2 x 1 = 2 to 2 x 10 = 20 using the for loop.
🛠️ What You Need
- Visual Studio Code or any C/C++ compiler
- Knowledge of for loops and arithmetic operations
📖 Introduction
A multiplication table shows the product of a number with numbers from 1 to 10 (or any range). In this practical, you will print the table of 2 using the for loop.
The table of 2 is:
- 2 x 1 = 2
- 2 x 2 = 4
- 2 x 3 = 6
- ... up to 2 x 10 = 20
You will use:
- A for loop to iterate from 1 to 10
- Multiplication to calculate the product (
2 * i)
- printf() to display the result in a formatted way
💡 Tip: The pattern for a multiplication table is: number x i = number * i, where i goes from 1 to 10.
📝 Steps
Step 1: Write the Program
- Open VS Code or any C compiler.
- Create a new file named table_of_2.c.
- Write the following code:
#include <stdio.h>
int main()
{
int i;
printf("========== TABLE OF 2 ==========\n");
for (i = 1; i <= 10; i++)
{
printf("2 x %d = %d\n", i, 2 * i);
}
printf("================================\n");
return 0;
}
Step 2: Explanation of the Code
- #include <stdio.h> — Includes the standard input/output library.
- int i; — Declares an integer variable
i as the loop counter.
- for (i = 1; i <= 10; i++) — The for loop runs 10 times (from 1 to 10).
- printf("2 x %d = %d\n", i, 2 * i); — Prints the multiplication in a formatted way.
Step 3: Save, Compile, and Run
- Save the file (Ctrl + S).
- Open the terminal and compile using the command:
gcc table_of_2.c -o table_of_2
./table_of_2
Sample Output:
========== TABLE OF 2 ==========
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
================================
Step 4: Modify the Program
- Change the number: Change
2 to 5 to print the table of 5.
- Change the range: Change
i <= 10 to i <= 15 to print up to 15.
- Take user input: Ask the user for the number and print its table.
int num;
printf("Enter a number: ");
scanf("%d", &num);
for (i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n", num, i, num * i);
}
📊 Table of 2
| Multiplication |
Result |
| 2 x 1 | 2 |
| 2 x 2 | 4 |
| 2 x 3 | 6 |
| 2 x 4 | 8 |
| 2 x 5 | 10 |
| 2 x 6 | 12 |
| 2 x 7 | 14 |
| 2 x 8 | 16 |
| 2 x 9 | 18 |
| 2 x 10 | 20 |
📊 For Loop Structure for Table
| Part |
Example |
Purpose |
| Initialization |
i = 1 |
Starts from 1 |
| Condition |
i <= 10 |
Runs until 10 |
| Increment |
i++ |
Increases by 1 each time |
| Body |
printf(...); |
Prints the table row |
📊 Alternative Methods
int i = 1;
while (i <= 10)
{
printf("2 x %d = %d\n", i, 2 * i);
i++;
}
int i = 1;
do
{
printf("2 x %d = %d\n", i, 2 * i);
i++;
} while (i <= 10);
for (i = 10; i >= 1; i--)
{
printf("2 x %d = %d\n", i, 2 * i);
}
📊 Observations
- The
for loop is used to iterate from 1 to 10.
- In each iteration, the product
2 * i is calculated and printed.
- The output is formatted as
2 x i = product.
- The loop runs exactly 10 times, printing each line of the table.
- You can easily change the number to print any multiplication table.
✅ Result
You have successfully written a C program that prints the multiplication table of 2 using the for loop. You have learned how to use loops to generate repetitive output like multiplication tables.
📌 Important: The for loop is ideal for printing tables because you know exactly how many iterations (10) you need. The multiplication is performed inside the loop.
💡 Tip: You can make the program more flexible by asking the user for the number and the range, and then printing the table using the for loop.
🎯 مقصد
ایک C/C++ پروگرام لکھنا جو for لوپ کا استعمال کرتے ہوئے 2 کا جدول 2 x 1 = 2 سے 2 x 10 = 20 تک پرنٹ کرے۔
🛠️ آپ کو کیا چاہیے
- Visual Studio Code یا کوئی بھی C/C++ کمپائلر
- for لوپ اور ریاضی کے عمل کی معلومات
📖 تعارف
ضرب کا جدول ایک عدد کو 1 سے 10 (یا کسی بھی حد) تک کے اعداد سے ضرب دینے کا نتیجہ دکھاتا ہے۔ اس عملی میں، آپ for لوپ کا استعمال کرتے ہوئے 2 کا جدول پرنٹ کریں گے۔
2 کا جدول یہ ہے:
- 2 x 1 = 2
- 2 x 2 = 4
- 2 x 3 = 6
- ... 2 x 10 = 20 تک
💡 مشورہ: ضرب کے جدول کا نمونہ یہ ہے: عدد x i = عدد * i، جہاں i 1 سے 10 تک جاتا ہے۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- table_of_2.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
int main()
{
int i;
printf("========== TABLE OF 2 ==========\n");
for (i = 1; i <= 10; i++)
{
printf("2 x %d = %d\n", i, 2 * i);
}
printf("================================\n");
return 0;
}
دوسرا قدم: کوڈ کی وضاحت
- #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
- int i; — لوپ کاؤنٹر کے لیے ایک انٹیجر متغیر
i کا اعلان کرتا ہے۔
- for (i = 1; i <= 10; i++) — for لوپ 10 بار چلتا ہے (1 سے 10 تک)۔
- printf("2 x %d = %d\n", i, 2 * i); — ضرب کو فارمیٹ شدہ طریقے سے پرنٹ کرتا ہے۔
تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں
- فائل کو محفوظ کریں (Ctrl + S)۔
- ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc table_of_2.c -o table_of_2
./table_of_2
نمونہ آؤٹ پٹ:
========== TABLE OF 2 ==========
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
================================
📊 2 کا جدول
| ضرب |
نتیجہ |
| 2 x 1 | 2 |
| 2 x 2 | 4 |
| 2 x 3 | 6 |
| 2 x 4 | 8 |
| 2 x 5 | 10 |
| 2 x 6 | 12 |
| 2 x 7 | 14 |
| 2 x 8 | 16 |
| 2 x 9 | 18 |
| 2 x 10 | 20 |
📊 مشاہدات
for لوپ 1 سے 10 تک تکرار کرنے کے لیے استعمال ہوتا ہے۔
- ہر تکرار میں،
2 * i کی ضرب کا حساب لگایا جاتا ہے اور پرنٹ کیا جاتا ہے۔
- آؤٹ پٹ کو
2 x i = product کے طور پر فارمیٹ کیا گیا ہے۔
- لوپ بالکل 10 بار چلتا ہے، جدول کی ہر لائن پرنٹ کرتا ہے۔
- آپ کسی بھی ضرب کا جدول پرنٹ کرنے کے لیے عدد کو آسانی سے تبدیل کر سکتے ہیں۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو for لوپ کا استعمال کرتے ہوئے 2 کا ضرب کا جدول پرنٹ کرتا ہے۔ آپ نے ضرب کے جدولوں جیسے بار بار آنے والے آؤٹ پٹ کو پیدا کرنے کے لیے لوپ کا استعمال سیکھ لیا ہے۔
📌 اہم: for لوپ جدول پرنٹ کرنے کے لیے بہترین ہے کیونکہ آپ کو معلوم ہے کہ آپ کو کتنی تکرار (10) کی ضرورت ہے۔ ضرب لوپ کے اندر کی جاتی ہے۔
💡 مشورہ: آپ صارف سے عدد اور حد پوچھ کر پروگرام کو زیادہ لچکدار بنا سکتے ہیں، اور پھر for لوپ کا استعمال کرتے ہوئے جدول پرنٹ کر سکتے ہیں۔