EZ

EZNotes

🎯 Aim

Write a C/C++ program that displays numbers 1 to 5 in a right‑angled triangular form using nested for loops.

🛠️ What You Need

  • Any C/C++ compiler (VS Code, Dev‑C++, Code::Blocks, etc.)
  • Basic understanding of for loops and nested iterations

📖 Introduction

In this practical, you will generate a triangle pattern where each row contains consecutive numbers starting from 1.

The target output looks like this:

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

Logic:

  • Outer loop – controls the number of rows (1 to 5).
  • Inner loop – controls the numbers in each row (1 to current row number).
  • Each row prints numbers from 1 up to the row index.
💡 Tip: In C, you can print numbers directly using printf("%d ", j) inside the inner loop. The outer loop variable i decides how many numbers appear in that row.

📝 Steps

Step 1: Write the Program

  1. Open your C compiler / VS Code.
  2. Create a new file named numbers_triangle.c.
  3. Write the following code:
/* Practical 51: Display numbers 1-5 in triangular form */ #include <stdio.h> int main() { int rows = 5; // number of rows printf("========== NUMBERS TRIANGLE (1-5) ==========\n"); for (int i = 1; i <= rows; i++) // outer loop: rows { for (int j = 1; j <= i; j++) // inner loop: print 1..i { printf("%d ", j); } printf("\n"); // new line after each row } printf("=============================================\n"); return 0; }

Step 2: Code Explanation

  1. #include <stdio.h> – standard I/O library.
  2. int rows = 5; – defines the number of rows (you can change it later).
  3. for (int i = 1; i <= rows; i++) – outer loop runs 5 times (i = 1 to 5).
  4. for (int j = 1; j <= i; j++) – inner loop prints numbers from 1 to i.
  5. printf("%d ", j); – prints the current number with a space.
  6. printf("\n"); – moves to next line after finishing a row.

Step 3: Save, Compile & Run

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

Run the program:

./numbers_triangle

Expected Output:

========== NUMBERS TRIANGLE (1-5) ========== 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 =============================================

Step 4: Variations & Enhancements

  1. Change row count: set rows = 10 to print numbers 1–10.
  2. Right‑aligned triangle: add spaces before numbers.
  3. Inverted triangle: modify outer loop to start from 5 and go down to 1.
/* Variation: Inverted triangle (5 4 3 2 1) */ for (int i = rows; i >= 1; i--) { for (int j = 1; j <= i; j++) printf("%d ", j); printf("\n"); }

📊 Execution Trace (Step‑by‑Step)

Row (i) Inner loop (j) Condition j <= i Printed numbers Row output
1j = 11 ≤ 1 ✅11
2j = 1,21≤2 ✅, 2≤2 ✅1, 21 2
3j = 1,2,3all true1,2,31 2 3
4j = 1..4all true1,2,3,41 2 3 4
5j = 1..5all true1,2,3,4,51 2 3 4 5

📊 Alternative Methods

/* Using while loops */ int i = 1; while (i <= rows) { int j = 1; while (j <= i) { printf("%d ", j); j++; } printf("\n"); i++; }
/* Using do-while loops */ int i = 1; do { int j = 1; do { printf("%d ", j); j++; } while (j <= i); printf("\n"); i++; } while (i <= rows);
/* Right‑aligned (with spaces) */ for (int i = 1; i <= rows; i++) { // print leading spaces for (int s = 1; s <= rows - i; s++) printf(" "); for (int j = 1; j <= i; j++) printf("%d ", j); printf("\n"); }

📊 Observations

  • Nested for loops are ideal for generating row‑column patterns.
  • The outer loop defines the row count; the inner loop prints values based on the row index.
  • Each row starts from 1 and ends at the current row number.
  • You can easily adjust the number of rows by changing the rows variable.

✅ Result

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.

📌 Important: The inner loop uses j <= i so that the number of printed values grows with each row. This is the core of the triangular pattern.
💡 Tip: Try changing 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 کے ذریعے ظاہر کرے۔

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

  • کوئی بھی C/C++ کمپائلر (VS Code، Dev‑C++، Code::Blocks وغیرہ)
  • for loops اور nested iterations کی بنیادی معلومات

📖 تعارف

اس عملی میں آپ ایک مثلثی نمونہ بنائیں گے جس میں ہر قطار میں یکے بعد دیگرے اعداد 1 سے شروع ہوتے ہیں۔

مطلوبہ آؤٹ پٹ اس طرح ہے:

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

منطق:

  • بیرونی لوپ – قطاروں کی تعداد کنٹرول کرتا ہے (1 سے 5)۔
  • اندرونی لوپ – ہر قطار میں اعداد کی تعداد کنٹرول کرتا ہے (1 سے موجودہ قطار نمبر تک)۔
  • ہر قطار 1 سے لے کر قطار کے انڈیکس تک اعداد پرنٹ کرتی ہے۔
💡 مشورہ: C میں، آپ printf("%d ", j) کے ذریعے اعداد پرنٹ کر سکتے ہیں۔ بیرونی لوپ کا متغیر i فیصلہ کرتا ہے کہ اس قطار میں کتنے اعداد آئیں گے۔

📝 اقدامات

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

  1. اپنا C کمپائلر / VS Code کھولیں۔
  2. numbers_triangle.c نام کی نئی فائل بنائیں۔
  3. نیچے دیا گیا کوڈ لکھیں:
/* Practical 51: Display numbers 1-5 in triangular form */ #include <stdio.h> int main() { int rows = 5; // number of rows printf("========== NUMBERS TRIANGLE (1-5) ==========\n"); for (int i = 1; i <= rows; i++) // outer loop: rows { for (int j = 1; j <= i; j++) // inner loop: print 1..i { printf("%d ", j); } printf("\n"); // new line after each row } printf("=============================================\n"); return 0; }

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

  1. #include <stdio.h> – معیاری I/O لائبریری۔
  2. int rows = 5; – قطاروں کی تعداد (آپ بعد میں تبدیل کر سکتے ہیں)۔
  3. for (int i = 1; i <= rows; i++) – بیرونی لوپ 5 بار چلتا ہے (i = 1 سے 5)۔
  4. for (int j = 1; j <= i; j++) – اندرونی لوپ 1 سے i تک اعداد پرنٹ کرتا ہے۔
  5. printf("%d ", j); – موجودہ عدد کو اسپیس کے ساتھ پرنٹ کرتا ہے۔
  6. printf("\n"); – قطار ختم ہونے پر اگلی لائن پر جاتا ہے۔

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

  1. فائل محفوظ کریں (Ctrl+S)۔
  2. ٹرمینل / کمانڈ پرامپٹ کھولیں اور کمپائل کریں:
gcc numbers_triangle.c -o numbers_triangle

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

./numbers_triangle

متوقع آؤٹ پٹ:

========== NUMBERS TRIANGLE (1-5) ========== 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 =============================================

چوتھا قدم: تغیرات اور اضافہ

  1. قطاروں کی تعداد تبدیل کریں: rows = 10 سیٹ کریں تو 1–10 تک پرنٹ ہوگا۔
  2. دائیں جانب ترتیب: اعداد سے پہلے اسپیس شامل کریں۔
  3. الٹی مثلث: بیرونی لوپ کو 5 سے 1 کی طرف چلائیں۔
/* تغیر: الٹی مثلث (5 4 3 2 1) */ for (int i = rows; i >= 1; i--) { for (int j = 1; j <= i; j++) printf("%d ", j); printf("\n"); }

📊 مرحلہ وار عمل (Execution Trace)

قطار (i) اندرونی لوپ (j) شرط j <= i پرنٹ شدہ اعداد قطار کا آؤٹ پٹ
1j = 11 ≤ 1 ✅11
2j = 1,21≤2 ✅, 2≤2 ✅1, 21 2
3j = 1,2,3سب درست1,2,31 2 3
4j = 1..4سب درست1,2,3,41 2 3 4
5j = 1..5سب درست1,2,3,4,51 2 3 4 5

📊 متبادل طریقے

/* while loops کا استعمال */ int i = 1; while (i <= rows) { int j = 1; while (j <= i) { printf("%d ", j); j++; } printf("\n"); i++; }
/* do-while loops کا استعمال */ int i = 1; do { int j = 1; do { printf("%d ", j); j++; } while (j <= i); printf("\n"); i++; } while (i <= rows);
/* دائیں جانب ترتیب (اسپیس کے ساتھ) */ for (int i = 1; i <= rows; i++) { for (int s = 1; s <= rows - i; s++) printf(" "); for (int j = 1; j <= i; j++) printf("%d ", j); printf("\n"); }

📊 مشاہدات

  • nested for loops قطار‑کالم کے نمونے بنانے کے لیے بہترین ہیں۔
  • بیرونی لوپ قطاروں کی تعداد طے کرتا ہے؛ اندرونی لوپ قطار کے انڈیکس کی بنیاد پر اعداد پرنٹ کرتا ہے۔
  • ہر قطار 1 سے شروع ہو کر موجودہ قطار نمبر پر ختم ہوتی ہے۔
  • آپ rows متغیر کو تبدیل کر کے قطاروں کی تعداد آسانی سے تبدیل کر سکتے ہیں۔

✅ نتیجہ

آپ نے کامیابی سے ایک C پروگرام بنایا جو اعداد 1 سے 5 کو دائیں‑زاویہ مثلث کی شکل میں nested for loops کے ذریعے پرنٹ کرتا ہے۔ آپ نے while، do‑while اور فارمیٹنگ کے مختلف اختیارات بھی دیکھے۔

📌 اہم: اندرونی لوپ j <= i استعمال کرتا ہے تاکہ ہر قطار کے ساتھ پرنٹ ہونے والے اعداد کی تعداد بڑھتی جائے۔ یہی مثلثی نمونے کی بنیاد ہے۔
💡 مشورہ: printf("%d ", j) کو printf("%d", j) (بغیر اسپیس) میں تبدیل کریں اور فرق دیکھیں۔ آپ \t (ٹیب) بھی استعمال کر سکتے ہیں۔
⬅ Back to Practical Notebooks