🎯 Aim
To write a C/C++ program that takes a number as input from the user and prints its multiplication table from 1 to 10 using the for loop.
🛠️ What You Need
- Visual Studio Code or any C/C++ compiler
- Knowledge of for loops, scanf(), and arithmetic operations
📖 Introduction
In this practical, you will create a program that asks the user for a number and then prints its multiplication table. This is a more flexible version of the previous practical because the user can enter any number they want.
For example, if the user enters 5, the program will print:
- 5 x 1 = 5
- 5 x 2 = 10
- 5 x 3 = 15
- ... up to 5 x 10 = 50
You will use:
- scanf() — to take the number from the user
- A for loop — to iterate from 1 to 10
- Multiplication — to calculate the product (
num * i)
- printf() — to display the result in a formatted way
💡 Tip: Taking user input makes the program more interactive and useful. The same program can print the table of any number without changing the code.
📝 Steps
Step 1: Write the Program
- Open VS Code or any C compiler.
- Create a new file named table_of_n.c.
- Write the following code:
#include <stdio.h>
int main()
{
int num, i;
printf("Enter a number to print its table: ");
scanf("%d", &num);
printf("\n========== TABLE OF %d ==========\n", num);
for (i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n", num, i, num * i);
}
printf("======================================\n");
return 0;
}
Step 2: Explanation of the Code
- #include <stdio.h> — Includes the standard input/output library.
- int num, i; — Declares two integer variables:
num for the user's number and i as the loop counter.
- scanf("%d", &num); — Reads the number from the user.
- for (i = 1; i <= 10; i++) — The for loop runs 10 times (from 1 to 10).
- printf("%d x %d = %d\n", num, i, num * i); — Prints the table 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_n.c -o table_of_n
./table_of_n
Sample Input & Output:
Enter a number to print its table: 7
========== TABLE OF 7 ==========
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
======================================
Enter a number to print its table: 12
========== TABLE OF 12 ==========
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120
======================================
Step 4: Modify the Program
- Change the range: Change
i <= 10 to i <= 15 to print up to 15.
- Print in reverse: Change the loop to
for (i = 10; i >= 1; i--) to print the table in reverse order.
- Add a limit: Ask the user for both the number and the limit.
int num, limit, i;
printf("Enter a number: ");
scanf("%d", &num);
printf("Enter the limit: ");
scanf("%d", &limit);
for (i = 1; i <= limit; i++)
{
printf("%d x %d = %d\n", num, i, num * i);
}
📊 Sample Tables
| Number |
Table (1 to 10) |
| 3 |
3, 6, 9, 12, 15, 18, 21, 24, 27, 30 |
| 5 |
5, 10, 15, 20, 25, 30, 35, 40, 45, 50 |
| 8 |
8, 16, 24, 32, 40, 48, 56, 64, 72, 80 |
| 10 |
10, 20, 30, 40, 50, 60, 70, 80, 90, 100 |
| 15 |
15, 30, 45, 60, 75, 90, 105, 120, 135, 150 |
📊 For Loop Structure
| 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("%d x %d = %d\n", num, i, num * i);
i++;
}
int i = 1;
do
{
printf("%d x %d = %d\n", num, i, num * i);
i++;
} while (i <= 10);
for (i = 10; i >= 1; i--)
{
printf("%d x %d = %d\n", num, i, num * i);
}
📊 Observations
- The program successfully takes a number as input from the user.
- The
for loop iterates from 1 to 10, printing each line of the table.
- The multiplication
num * i is calculated inside the loop.
- The output is formatted as
num x i = product.
- The program is flexible and can print the table of any number entered by the user.
✅ Result
You have successfully written a C program that takes a number as input from the user and prints its multiplication table using the for loop. You have learned how to make your programs interactive by taking user input.
📌 Important: The scanf() function is used to take input from the user. Always use & before the variable name in scanf().
💡 Tip: You can extend this program by asking the user for both the number and the limit (e.g., up to 20) to make it even more flexible.
🎯 مقصد
ایک C/C++ پروگرام لکھنا جو صارف سے ایک عدد ان پٹ کے طور پر لے اور for لوپ کا استعمال کرتے ہوئے اس کا ضرب کا جدول 1 سے 10 تک پرنٹ کرے۔
🛠️ آپ کو کیا چاہیے
- Visual Studio Code یا کوئی بھی C/C++ کمپائلر
- for لوپ، scanf()، اور ریاضی کے عمل کی معلومات
📖 تعارف
اس عملی میں، آپ ایک پروگرام بنائیں گے جو صارف سے ایک عدد پوچھتا ہے اور پھر اس کا ضرب کا جدول پرنٹ کرتا ہے۔ یہ پچھلی عملی کا ایک زیادہ لچکدار ورژن ہے کیونکہ صارف کوئی بھی عدد درج کر سکتا ہے۔
مثال کے طور پر، اگر صارف 5 درج کرتا ہے، تو پروگرام پرنٹ کرے گا:
- 5 x 1 = 5
- 5 x 2 = 10
- 5 x 3 = 15
- ... 5 x 10 = 50 تک
💡 مشورہ: صارف سے ان پٹ لینا پروگرام کو زیادہ انٹرایکٹو اور مفید بناتا ہے۔ ایک ہی پروگرام کوڈ کو تبدیل کیے بغیر کسی بھی عدد کا جدول پرنٹ کر سکتا ہے۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- table_of_n.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
int main()
{
int num, i;
printf("Enter a number to print its table: ");
scanf("%d", &num);
printf("\n========== TABLE OF %d ==========\n", num);
for (i = 1; i <= 10; i++)
{
printf("%d x %d = %d\n", num, i, num * i);
}
printf("======================================\n");
return 0;
}
دوسرا قدم: کوڈ کی وضاحت
- #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
- int num, i; — دو انٹیجر متغیرات کا اعلان کرتا ہے:
num صارف کے عدد کے لیے اور i لوپ کاؤنٹر کے لیے۔
- scanf("%d", &num); — صارف سے عدد پڑھتا ہے۔
- for (i = 1; i <= 10; i++) — for لوپ 10 بار چلتا ہے (1 سے 10 تک)۔
- printf("%d x %d = %d\n", num, i, num * i); — جدول کو فارمیٹ شدہ طریقے سے پرنٹ کرتا ہے۔
تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں
- فائل کو محفوظ کریں (Ctrl + S)۔
- ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc table_of_n.c -o table_of_n
./table_of_n
نمونہ ان پٹ اور آؤٹ پٹ:
Enter a number to print its table: 7
========== TABLE OF 7 ==========
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
======================================
📊 نمونہ جدول
| عدد |
جدول (1 سے 10) |
| 3 |
3, 6, 9, 12, 15, 18, 21, 24, 27, 30 |
| 5 |
5, 10, 15, 20, 25, 30, 35, 40, 45, 50 |
| 8 |
8, 16, 24, 32, 40, 48, 56, 64, 72, 80 |
| 10 |
10, 20, 30, 40, 50, 60, 70, 80, 90, 100 |
| 15 |
15, 30, 45, 60, 75, 90, 105, 120, 135, 150 |
📊 مشاہدات
- پروگرام کامیابی سے صارف سے ایک عدد ان پٹ کے طور پر لیتا ہے۔
for لوپ 1 سے 10 تک تکرار کرتا ہے، جدول کی ہر لائن پرنٹ کرتا ہے۔
- ضرب
num * i لوپ کے اندر حساب کی جاتی ہے۔
- آؤٹ پٹ کو
num x i = product کے طور پر فارمیٹ کیا گیا ہے۔
- پروگرام لچکدار ہے اور صارف کے درج کردہ کسی بھی عدد کا جدول پرنٹ کر سکتا ہے۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو صارف سے ایک عدد ان پٹ کے طور پر لیتا ہے اور for لوپ کا استعمال کرتے ہوئے اس کا ضرب کا جدول پرنٹ کرتا ہے۔ آپ نے صارف سے ان پٹ لے کر اپنے پروگراموں کو انٹرایکٹو بنانا سیکھ لیا ہے۔
📌 اہم: scanf() فنکشن صارف سے ان پٹ لینے کے لیے استعمال ہوتا ہے۔ scanf() میں ہمیشہ متغیر کے نام سے پہلے & استعمال کریں۔
💡 مشورہ: آپ صارف سے عدد اور حد (مثال: 20 تک) دونوں پوچھ کر اس پروگرام کو مزید لچکدار بنا سکتے ہیں۔