🎯 Aim
To write a C/C++ program that displays the word "PAKISTAN" in the form of a triangle using nested for loops.
🛠️ What You Need
- Visual Studio Code or any C/C++ compiler
- Knowledge of nested for loops and string indexing
📖 Introduction
In this practical, you will create a triangle pattern using the letters of the word "PAKISTAN".
The pattern looks like this:
P
PA
PAK
PAKI
PAKIS
PAKIST
PAKISTA
PAKISTAN
The logic is:
- Outer loop — controls the number of rows (1 to 8)
- Inner loop — controls the number of characters in each row (1 to i)
- String indexing — prints characters from the word "PAKISTAN"
💡 Tip: In C, strings are arrays of characters. You can access individual characters using index numbers starting from 0. For example, "PAKISTAN"[0] is 'P', "PAKISTAN"[1] is 'A', etc.
📝 Steps
Step 1: Write the Program
- Open VS Code or any C compiler.
- Create a new file named pakistan_triangle.c.
- Write the following code:
#include <stdio.h>
#include <string.h>
int main()
{
char word[] = "PAKISTAN";
int len = strlen(word);
printf("========== PAKISTAN TRIANGLE ==========\n");
for (int i = 1; i <= len; i++)
{
for (int j = 0; j < i; j++)
{
printf("%c", word[j]);
}
printf("\n");
}
printf("========================================\n");
return 0;
}
Step 2: Explanation of the Code
- #include <stdio.h> — Includes the standard input/output library.
- #include <string.h> — Includes the string library for
strlen().
- char word[] = "PAKISTAN"; — Declares a string containing the word.
- int len = strlen(word); — Calculates the length of the word (8).
- for (int i = 1; i <= len; i++) — Outer loop runs 8 times (for 8 rows).
- for (int j = 0; j < i; j++) — Inner loop runs from 0 to i-1.
- printf("%c", word[j]); — Prints the character at index j from the word.
- printf("\n"); — Moves to the next line after each row.
Step 3: Save, Compile, and Run
- Save the file (Ctrl + S).
- Open the terminal and compile using the command:
gcc pakistan_triangle.c -o pakistan_triangle
./pakistan_triangle
Sample Output:
========== PAKISTAN TRIANGLE ==========
P
PA
PAK
PAKI
PAKIS
PAKIST
PAKISTA
PAKISTAN
========================================
Step 4: Modify the Program
- Change the word: Change
"PAKISTAN" to "PAKISTAN ZINDABAD" or any other word.
- Take user input: Ask the user to enter a word and print its triangle.
- Print a different pattern: Print the triangle in reverse order or as a pyramid.
char word[100];
printf("Enter a word: ");
scanf("%s", word);
int len = strlen(word);
for (int i = 1; i <= len; i++)
{
for (int j = 0; j < i; j++)
{
printf("%c", word[j]);
}
printf("\n");
}
📊 Nested Loop Execution (Step-by-Step)
| Row (i) |
Inner Loop (j) |
Condition j < i |
Character Printed |
Row Output |
| 1 |
j = 0 |
0 < 1 ✅ |
word[0] = 'P' |
P |
| 2 |
j = 0, 1 |
0 < 2 ✅, 1 < 2 ✅ |
word[0]='P', word[1]='A' |
PA |
| 3 |
j = 0, 1, 2 |
0 < 3 ✅, 1 < 3 ✅, 2 < 3 ✅ |
word[0]='P', word[1]='A', word[2]='K' |
PAK |
| 4 |
j = 0, 1, 2, 3 |
All true |
P, A, K, I |
PAKI |
| 5 |
j = 0, 1, 2, 3, 4 |
All true |
P, A, K, I, S |
PAKIS |
| 6 |
j = 0 to 5 |
All true |
P, A, K, I, S, T |
PAKIST |
| 7 |
j = 0 to 6 |
All true |
P, A, K, I, S, T, A |
PAKISTA |
| 8 |
j = 0 to 7 |
All true |
P, A, K, I, S, T, A, N |
PAKISTAN |
📊 Pattern Variations
| Pattern |
Code Modification |
Output |
| Right Triangle |
printf("%c", word[j]); |
P PA PAK PAKI PAKIS PAKIST PAKISTA PAKISTAN |
| With Spaces |
printf("%c ", word[j]); |
P P A P A K P A K I P A K I S P A K I S T P A K I S T A P A K I S T A N |
| Inverted Triangle |
for (int i = len; i >= 1; i--) |
PAKISTAN PAKISTA PAKIST PAKIS PAKI PAK PA P |
| Different Word |
char word[] = "C++"; |
C C+ C++ |
📊 Alternative Methods
int i = 1;
while (i <= len)
{
int j = 0;
while (j < i)
{
printf("%c", word[j]);
j++;
}
printf("\n");
i++;
}
int i = 1;
do
{
int j = 0;
do
{
printf("%c", word[j]);
j++;
} while (j < i);
printf("\n");
i++;
} while (i <= len);
for (int i = 1; i <= len; i++)
{
for (int sp = 1; sp <= len - i; sp++)
{
printf(" ");
}
for (int j = 0; j < i; j++)
{
printf("%c", word[j]);
}
printf("\n");
}
📊 Observations
- Nested for loops are used to create the triangle pattern.
- The outer loop controls the number of rows (8 rows for "PAKISTAN").
- The inner loop controls the number of characters in each row.
- String indexing (
word[j]) is used to print the characters.
- Each row prints one more character than the previous row.
- The pattern forms a right triangle with the word "PAKISTAN".
✅ Result
You have successfully written a C program that displays the word "PAKISTAN" in the form of a triangle using nested for loops. You have learned how to use string indexing and nested loops to create patterns.
📌 Important: String indexing starts from 0. The character at position j in the string is accessed using word[j]. The length of the string can be found using strlen() from the string.h library.
💡 Tip: You can create many different patterns by modifying the loop conditions and the characters printed. Try creating a pyramid, a diamond, or a reverse triangle!
🎯 مقصد
ایک C/C++ پروگرام لکھنا جو "PAKISTAN" کو مثلث کی شکل میں nested for loops کا استعمال کرتے ہوئے ظاہر کرے۔
🛠️ آپ کو کیا چاہیے
- Visual Studio Code یا کوئی بھی C/C++ کمپائلر
- nested for loops اور سٹرنگ انڈیکسنگ کی معلومات
📖 تعارف
اس عملی میں، آپ "PAKISTAN" کے حروف کا استعمال کرتے ہوئے مثلث کا نمونہ بنائیں گے۔
یہ نمونہ اس طرح نظر آتا ہے:
P
PA
PAK
PAKI
PAKIS
PAKIST
PAKISTA
PAKISTAN
منطق یہ ہے:
- بیرونی لوپ — قطاروں کی تعداد کو کنٹرول کرتا ہے (1 سے 8 تک)
- اندرونی لوپ — ہر قطار میں حروف کی تعداد کو کنٹرول کرتا ہے (1 سے i تک)
- سٹرنگ انڈیکسنگ — "PAKISTAN" سے حروف پرنٹ کرتا ہے
💡 مشورہ: C میں، سٹرنگز حروف کی اری ہیں۔ آپ 0 سے شروع ہونے والے انڈیکس نمبرز کا استعمال کرتے ہوئے انفرادی حروف تک رسائی حاصل کر سکتے ہیں۔ مثال کے طور پر، "PAKISTAN"[0] 'P' ہے، "PAKISTAN"[1] 'A' ہے، وغیرہ۔
📝 اقدامات
پہلا قدم: پروگرام لکھیں
- VS Code یا کوئی بھی C کمپائلر کھولیں۔
- pakistan_triangle.c نام کی ایک نئی فائل بنائیں۔
- نیچے دیا گیا کوڈ لکھیں:
#include <stdio.h>
#include <string.h>
int main()
{
char word[] = "PAKISTAN";
int len = strlen(word);
printf("========== PAKISTAN TRIANGLE ==========\n");
for (int i = 1; i <= len; i++)
{
for (int j = 0; j < i; j++)
{
printf("%c", word[j]);
}
printf("\n");
}
printf("========================================\n");
return 0;
}
دوسرا قدم: کوڈ کی وضاحت
- #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
- #include <string.h> —
strlen() کے لیے سٹرنگ لائبریری شامل کرتا ہے۔
- char word[] = "PAKISTAN"; — لفظ پر مشتمل سٹرنگ کا اعلان کرتا ہے۔
- int len = strlen(word); — لفظ کی لمبائی (8) کا حساب کرتا ہے۔
- for (int i = 1; i <= len; i++) — بیرونی لوپ 8 بار چلتا ہے (8 قطاروں کے لیے)۔
- for (int j = 0; j < i; j++) — اندرونی لوپ 0 سے i-1 تک چلتا ہے۔
- printf("%c", word[j]); — لفظ سے انڈیکس j پر حرف پرنٹ کرتا ہے۔
- printf("\n"); — ہر قطار کے بعد اگلی لائن پر جاتا ہے۔
تیسرا قدم: محفوظ کریں، کمپائل کریں اور چلائیں
- فائل کو محفوظ کریں (Ctrl + S)۔
- ٹرمینل کھولیں اور نیچے دی گئی کمانڈ استعمال کرتے ہوئے کمپائل کریں:
gcc pakistan_triangle.c -o pakistan_triangle
./pakistan_triangle
نمونہ آؤٹ پٹ:
========== PAKISTAN TRIANGLE ==========
P
PA
PAK
PAKI
PAKIS
PAKIST
PAKISTA
PAKISTAN
========================================
📊 nested لوپ کا مرحلہ وار عمل
| قطار (i) |
اندرونی لوپ (j) |
شرط j < i |
پرنٹ کردہ حرف |
قطار کا آؤٹ پٹ |
| 1 |
j = 0 |
0 < 1 ✅ |
word[0] = 'P' |
P |
| 2 |
j = 0, 1 |
0 < 2 ✅, 1 < 2 ✅ |
word[0]='P', word[1]='A' |
PA |
| 3 |
j = 0, 1, 2 |
0 < 3 ✅, 1 < 3 ✅, 2 < 3 ✅ |
word[0]='P', word[1]='A', word[2]='K' |
PAK |
| 4 |
j = 0, 1, 2, 3 |
سب درست |
P, A, K, I |
PAKI |
| 5 |
j = 0, 1, 2, 3, 4 |
سب درست |
P, A, K, I, S |
PAKIS |
| 6 |
j = 0 to 5 |
سب درست |
P, A, K, I, S, T |
PAKIST |
| 7 |
j = 0 to 6 |
سب درست |
P, A, K, I, S, T, A |
PAKISTA |
| 8 |
j = 0 to 7 |
سب درست |
P, A, K, I, S, T, A, N |
PAKISTAN |
📊 مشاہدات
- nested for loops کو مثلث کا نمونہ بنانے کے لیے استعمال کیا جاتا ہے۔
- بیرونی لوپ قطاروں کی تعداد کو کنٹرول کرتا ہے ("PAKISTAN" کے لیے 8 قطاریں)۔
- اندرونی لوپ ہر قطار میں حروف کی تعداد کو کنٹرول کرتا ہے۔
- سٹرنگ انڈیکسنگ (
word[j]) حروف پرنٹ کرنے کے لیے استعمال ہوتی ہے۔
- ہر قطار پچھلی قطار سے ایک حرف زیادہ پرنٹ کرتی ہے۔
- یہ نمونہ "PAKISTAN" کے ساتھ دائیں زاویہ مثلث بناتا ہے۔
✅ نتیجہ
آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو "PAKISTAN" کو مثلث کی شکل میں nested for loops کا استعمال کرتے ہوئے ظاہر کرتا ہے۔ آپ نے نمونے بنانے کے لیے سٹرنگ انڈیکسنگ اور nested loops کا استعمال سیکھ لیا ہے۔
📌 اہم: سٹرنگ انڈیکسنگ 0 سے شروع ہوتی ہے۔ سٹرنگ میں پوزیشن j پر حرف کو word[j] کے ساتھ رسائی حاصل کی جاتی ہے۔ سٹرنگ کی لمبائی string.h لائبریری سے strlen() کے ساتھ معلوم کی جا سکتی ہے۔
💡 مشورہ: آپ لوپ کی شرائط اور پرنٹ کیے جانے والے حروف کو تبدیل کرکے بہت سے مختلف نمونے بنا سکتے ہیں۔ اہرام، ہیرا، یا الٹی مثلث بنانے کی کوشش کریں!