Learn how to display a right‑angle triangle of numbers from 1 to 5 using nested for loops.
Write a C/C++ program that displays numbers 1 to 5 in a right‑angled triangular form using nested for loops.
for loops and nested iterationsIn this practical, you will generate a triangle pattern where each row contains consecutive numbers starting from 1.
The target output looks like this:
Logic:
printf("%d ", j) inside the inner loop. The outer loop variable i decides how many numbers appear in that row.
Run the program:
Expected Output:
rows = 10 to print numbers 1–10.| Row (i) | Inner loop (j) | Condition j <= i |
Printed numbers | Row output |
|---|---|---|---|---|
| 1 | j = 1 | 1 ≤ 1 ✅ | 1 | 1 |
| 2 | j = 1,2 | 1≤2 ✅, 2≤2 ✅ | 1, 2 | 1 2 |
| 3 | j = 1,2,3 | all true | 1,2,3 | 1 2 3 |
| 4 | j = 1..4 | all true | 1,2,3,4 | 1 2 3 4 |
| 5 | j = 1..5 | all true | 1,2,3,4,5 | 1 2 3 4 5 |
for loops are ideal for generating row‑column patterns.rows variable.You have successfully created a C program that prints numbers 1 to 5 in a right‑triangle pattern using nested for loops. You also explored variations using while, do‑while, and formatting options.
j <= i so that the number of printed values grows with each row. This is the core of the triangular pattern.
printf("%d ", j) to printf("%d", j) (without space) and see the difference. You can also use \t (tab) for alignment.
ایک C/C++ پروگرام لکھیں جو اعداد 1 سے 5 کو مثلثی شکل میں nested for loops کے ذریعے ظاہر کرے۔
for loops اور nested iterations کی بنیادی معلوماتاس عملی میں آپ ایک مثلثی نمونہ بنائیں گے جس میں ہر قطار میں یکے بعد دیگرے اعداد 1 سے شروع ہوتے ہیں۔
مطلوبہ آؤٹ پٹ اس طرح ہے:
منطق:
printf("%d ", j) کے ذریعے اعداد پرنٹ کر سکتے ہیں۔ بیرونی لوپ کا متغیر i فیصلہ کرتا ہے کہ اس قطار میں کتنے اعداد آئیں گے۔
پروگرام چلائیں:
متوقع آؤٹ پٹ:
rows = 10 سیٹ کریں تو 1–10 تک پرنٹ ہوگا۔| قطار (i) | اندرونی لوپ (j) | شرط j <= i |
پرنٹ شدہ اعداد | قطار کا آؤٹ پٹ |
|---|---|---|---|---|
| 1 | j = 1 | 1 ≤ 1 ✅ | 1 | 1 |
| 2 | j = 1,2 | 1≤2 ✅, 2≤2 ✅ | 1, 2 | 1 2 |
| 3 | j = 1,2,3 | سب درست | 1,2,3 | 1 2 3 |
| 4 | j = 1..4 | سب درست | 1,2,3,4 | 1 2 3 4 |
| 5 | j = 1..5 | سب درست | 1,2,3,4,5 | 1 2 3 4 5 |
for loops قطار‑کالم کے نمونے بنانے کے لیے بہترین ہیں۔rows متغیر کو تبدیل کر کے قطاروں کی تعداد آسانی سے تبدیل کر سکتے ہیں۔آپ نے کامیابی سے ایک C پروگرام بنایا جو اعداد 1 سے 5 کو دائیں‑زاویہ مثلث کی شکل میں nested for loops کے ذریعے پرنٹ کرتا ہے۔ آپ نے while، do‑while اور فارمیٹنگ کے مختلف اختیارات بھی دیکھے۔
j <= i استعمال کرتا ہے تاکہ ہر قطار کے ساتھ پرنٹ ہونے والے اعداد کی تعداد بڑھتی جائے۔ یہی مثلثی نمونے کی بنیاد ہے۔
printf("%d ", j) کو printf("%d", j) (بغیر اسپیس) میں تبدیل کریں اور فرق دیکھیں۔ آپ \t (ٹیب) بھی استعمال کر سکتے ہیں۔