Program in C to Print Number Triangle

Program in C to Print Number Triangle

A number triangle is a pattern where numbers are arranged in a triangular format, with each row containing a sequence of numbers. Printing such patterns helps in understanding nested loops and number sequencing in C. This program demonstrates how to print a simple increasing number triangle.

Understanding The Problem

The program should take the number of rows as input and print a triangle of numbers. Each row contains numbers starting from 1 up to the row number, forming a triangular structure. Nested loops are used to handle rows and numbers in each row.

Steps to Solve the Problem:

  • Take the number of rows from the user.
  • Use an outer loop to iterate through each row.
  • Use an inner loop to print numbers in increasing order.
  • Move to a new line after printing each row.

Solution: Using Nested Loops

Nested loops allow printing each number in a row while moving to the next row after completion.

#include <stdio.h>

int main() {

    int rows;

    printf("Enter number of rows: ");
    scanf("%d", &rows);

    for(int i = 1; i <= rows; i++) {

        for(int j = 1; j <= i; j++) {
            printf("%d ", j);
        }

        printf("\n");

    }

    return 0;

}

In this program, the outer loop controls the rows from 1 to rows. The inner loop prints numbers from 1 up to the current row number i. After each row, a newline is printed, forming a number triangle.

Number Triangle

FAQs

Q1: Can the numbers start from a number other than 1?
Yes, you can adjust the inner loop to start from any number to create a triangle starting with that number.

Q2: Can the triangle be inverted?
Yes, by adjusting the outer loop to start from the maximum row number and decrement, you can print an inverted number triangle.

Q3: Can this pattern use other sequences?
Yes, you can use arithmetic sequences, Fibonacci numbers, or even letters instead of numbers.

Conclusion

This program demonstrates how to print a simple number triangle using nested loops. By controlling the outer and inner loops carefully, you can create various triangular number patterns. The logic can be extended to inverted triangles, Pascal’s triangle, or other numeric patterns.

References & Additional Resources

Scroll to Top