EZ Notes Logo

EZNotes

🎯 Aim

To write a C/C++ program that displays a right triangle of asterisks using nested while loops.

🛠️ What You Need

  • Visual Studio Code or any C/C++ compiler
  • Knowledge of nested loops (while loops)

📖 Introduction

In this practical, you will create a right triangle pattern of asterisks (*) using nested while loops.

The pattern looks like this for 5 rows:

* ** *** **** *****

The logic is:

  • Outer loop — controls the number of rows (i from 1 to 5)
  • Inner loop — controls the number of asterisks in each row (j from 1 to i)
💡 Tip: Nested loops are loops inside loops. The outer loop runs once for each row, and the inner loop runs multiple times to print the asterisks in that row.

📝 Steps

Step 1: Write the Program

  1. Open VS Code or any C compiler.
  2. Create a new file named triangle_while.c.
  3. Write the following code:
/* Practical 49: Program to display right triangle of asterisks using nested while loops */ #include <stdio.h> int main() { int rows = 5; // Number of rows int i = 1; // Outer loop counter /* ============================================ Using nested while loops to display right triangle ============================================ */ printf("========== RIGHT TRIANGLE ==========\n"); while (i <= rows) { int j = 1; // Inner loop counter while (j <= i) { printf("*"); j++; } printf("\n"); // Move to next line i++; } printf("====================================\n"); return 0; }

Step 2: Explanation of the Code

  1. #include <stdio.h> — Includes the standard input/output library.
  2. int rows = 5; — Sets the number of rows to 5.
  3. int i = 1; — Initializes the outer loop counter to 1.
  4. while (i <= rows) — Outer loop runs 5 times (for each row).
  5. int j = 1; — Initializes the inner loop counter to 1.
  6. while (j <= i) — Inner loop runs i times, printing i asterisks.
  7. printf("*"); — Prints a single asterisk.
  8. j++; — Increments the inner loop counter.
  9. printf("\n"); — Moves to the next line after each row.
  10. i++; — Increments the outer loop counter.

Step 3: Save, Compile, and Run

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

Run the program:

./triangle_while

Sample Output:

========== RIGHT TRIANGLE ========== * ** *** **** ***** ====================================

Step 4: Modify the Program

  1. Change the number of rows: Change rows = 5 to rows = 8 to print 8 rows.
  2. Take user input: Ask the user for the number of rows.
  3. Print with spaces: Add spaces between asterisks.
  4. Print a different character: Change * to # or any other character.
/* Modified: Take user input for number of rows */ int rows, i = 1; printf("Enter number of rows: "); scanf("%d", &rows); while (i <= rows) { int j = 1; while (j <= i) { printf("*"); j++; } printf("\n"); i++; }

📊 Nested Loop Execution (Step-by-Step)

Row (i) Inner Loop (j) Condition j <= i Output
1 j = 1 1 <= 1 ✅ *
j = 2 2 <= 1 ❌ Newline
2 j = 1 1 <= 2 ✅ *
j = 2 2 <= 2 ✅ *
j = 3 3 <= 2 ❌ Newline
3 j = 1 1 <= 3 ✅ *
j = 2 2 <= 3 ✅ *
j = 3 3 <= 3 ✅ *
j = 4 4 <= 3 ❌ Newline
4 j = 1 1 <= 4 ✅ *
j = 2 2 <= 4 ✅ *
j = 3 3 <= 4 ✅ *
j = 4 4 <= 4 ✅ *
j = 5 5 <= 4 ❌ Newline
5 j = 1 1 <= 5 ✅ *
j = 2 2 <= 5 ✅ *
j = 3 3 <= 5 ✅ *
j = 4 4 <= 5 ✅ *
j = 5 5 <= 5 ✅ *
j = 6 6 <= 5 ❌ Newline

📊 Pattern Variations

Pattern Code Output (Rows=5)
Right Triangle printf("*"); *
**
***
****
*****
With Spaces printf("* "); *
* *
* * *
* * * *
* * * * *
Different Character printf("#"); #
##
###
####
#####

📊 Alternative Methods

/* Alternative 1: Using for loops */ for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { printf("*"); } printf("\n"); }
/* Alternative 2: Using do-while loops */ int i = 1; do { int j = 1; do { printf("*"); j++; } while (j <= i); printf("\n"); i++; } while (i <= rows);
/* Alternative 3: Inverted triangle (right justified) */ int i = rows; while (i >= 1) { int j = 1; while (j <= i) { printf("*"); j++; } printf("\n"); i--; }

📊 Observations

  • Nested while loops are used to create the pattern.
  • The outer loop controls the number of rows (5 rows).
  • The inner loop controls the number of asterisks in each row.
  • In row 1, the inner loop runs 1 time (prints 1 asterisk).
  • In row 2, the inner loop runs 2 times (prints 2 asterisks).
  • In row 5, the inner loop runs 5 times (prints 5 asterisks).
  • The pattern forms a right triangle.

✅ Result

You have successfully written a C program that displays a right triangle of asterisks using nested while loops. You have learned how to use nested loops to create patterns and shapes.

📌 Important: In nested loops, the inner loop completes all its iterations for each single iteration of the outer loop. This is why the inner loop prints i asterisks in row i.
💡 Tip: You can create many different patterns by changing the loop conditions and the characters printed. Try creating an inverted triangle, a square, or a pyramid!

🎯 مقصد

ایک C/C++ پروگرام لکھنا جو nested while loops کا استعمال کرتے ہوئے ستاروں کا دائیں زاویہ مثلث ظاہر کرے۔

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

  • Visual Studio Code یا کوئی بھی C/C++ کمپائلر
  • nested loops (while loops) کی معلومات

📖 تعارف

اس عملی میں، آپ nested while loops کا استعمال کرتے ہوئے ستاروں (*) کا دائیں زاویہ مثلث بنائیں گے۔

5 قطاروں کے لیے یہ نمونہ اس طرح نظر آتا ہے:

* ** *** **** *****

منطق یہ ہے:

  • بیرونی لوپ — قطاروں کی تعداد کو کنٹرول کرتا ہے (i 1 سے 5 تک)
  • اندرونی لوپ — ہر قطار میں ستاروں کی تعداد کو کنٹرول کرتا ہے (j 1 سے i تک)
💡 مشورہ: Nested loops اندر لوپ ہیں۔ بیرونی لوپ ہر قطار کے لیے ایک بار چلتا ہے، اور اندرونی لوپ اس قطار میں ستارے پرنٹ کرنے کے لیے کئی بار چلتا ہے۔

📝 اقدامات

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

  1. VS Code یا کوئی بھی C کمپائلر کھولیں۔
  2. triangle_while.c نام کی ایک نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 49: Program to display right triangle of asterisks using nested while loops */ #include <stdio.h> int main() { int rows = 5; // Number of rows int i = 1; // Outer loop counter /* ============================================ Using nested while loops to display right triangle ============================================ */ printf("========== RIGHT TRIANGLE ==========\n"); while (i <= rows) { int j = 1; // Inner loop counter while (j <= i) { printf("*"); j++; } printf("\n"); // Move to next line i++; } printf("====================================\n"); return 0; }

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

  1. #include <stdio.h> — معیاری ان پٹ/آؤٹ پٹ لائبریری شامل کرتا ہے۔
  2. int rows = 5; — قطاروں کی تعداد 5 مقرر کرتا ہے۔
  3. int i = 1; — بیرونی لوپ کاؤنٹر کو 1 سے شروع کرتا ہے۔
  4. while (i <= rows) — بیرونی لوپ 5 بار چلتا ہے (ہر قطار کے لیے)۔
  5. int j = 1; — اندرونی لوپ کاؤنٹر کو 1 سے شروع کرتا ہے۔
  6. while (j <= i) — اندرونی لوپ i بار چلتا ہے، i ستارے پرنٹ کرتا ہے۔
  7. printf("*"); — ایک ستارہ پرنٹ کرتا ہے۔
  8. j++; — اندرونی لوپ کاؤنٹر کو بڑھاتا ہے۔
  9. printf("\n"); — ہر قطار کے بعد اگلی لائن پر جاتا ہے۔
  10. i++; — بیرونی لوپ کاؤنٹر کو بڑھاتا ہے۔

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

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

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

./triangle_while

نمونہ آؤٹ پٹ:

========== RIGHT TRIANGLE ========== * ** *** **** ***** ====================================

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

قطار (i) اندرونی لوپ (j) شرط j <= i آؤٹ پٹ
1 j = 1 1 <= 1 ✅ *
j = 2 2 <= 1 ❌ نئی لائن
2 j = 1 1 <= 2 ✅ *
j = 2 2 <= 2 ✅ *
j = 3 3 <= 2 ❌ نئی لائن
3 j = 1 1 <= 3 ✅ *
j = 2 2 <= 3 ✅ *
j = 3 3 <= 3 ✅ *
j = 4 4 <= 3 ❌ نئی لائن
4 j = 1 1 <= 4 ✅ *
j = 2 2 <= 4 ✅ *
j = 3 3 <= 4 ✅ *
j = 4 4 <= 4 ✅ *
j = 5 5 <= 4 ❌ نئی لائن
5 j = 1 1 <= 5 ✅ *
j = 2 2 <= 5 ✅ *
j = 3 3 <= 5 ✅ *
j = 4 4 <= 5 ✅ *
j = 5 5 <= 5 ✅ *
j = 6 6 <= 5 ❌ نئی لائن

📊 مشاہدات

  • nested while loops کو نمونہ بنانے کے لیے استعمال کیا جاتا ہے۔
  • بیرونی لوپ قطاروں کی تعداد کو کنٹرول کرتا ہے (5 قطاریں)۔
  • اندرونی لوپ ہر قطار میں ستاروں کی تعداد کو کنٹرول کرتا ہے۔
  • قطار 1 میں، اندرونی لوپ 1 بار چلتا ہے (1 ستارہ پرنٹ کرتا ہے)۔
  • قطار 2 میں، اندرونی لوپ 2 بار چلتا ہے (2 ستارے پرنٹ کرتا ہے)۔
  • قطار 5 میں، اندرونی لوپ 5 بار چلتا ہے (5 ستارے پرنٹ کرتا ہے)۔
  • یہ نمونہ دائیں زاویہ مثلث بناتا ہے۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام لکھا ہے جو nested while loops کا استعمال کرتے ہوئے ستاروں کا دائیں زاویہ مثلث ظاہر کرتا ہے۔ آپ نے نمونے اور اشکال بنانے کے لیے nested loops کا استعمال سیکھ لیا ہے۔

📌 اہم: nested loops میں، اندرونی لوپ بیرونی لوپ کی ہر ایک تکرار کے لیے اپنی تمام تکرار مکمل کرتا ہے۔ یہی وجہ ہے کہ اندرونی لوپ قطار i میں i ستارے پرنٹ کرتا ہے۔
💡 مشورہ: آپ لوپ کی شرائط اور پرنٹ کیے جانے والے حروف کو تبدیل کرکے بہت سے مختلف نمونے بنا سکتے ہیں۔ الٹی مثلث، مربع، یا اہرام بنانے کی کوشش کریں!
⬅ Back to Practical Notebooks