Program in C to Print Right-Angled Triangle

Program in C to Print Right-Angled Triangle

A right-angled triangle pattern is a simple triangular arrangement where the right angle is typically at the bottom-left. Printing such patterns helps beginners practice nested loops and alignment in C.

Understanding The Problem

The program should take the number of rows as input and print a right-angled triangle of stars. Each row contains an increasing number of stars, starting from 1 at the top and increasing by 1 in each subsequent row. Nested loops handle the number of stars per 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 stars corresponding to the current row number.
  • Move to a new line after printing each row.

Solution: Using Nested Loops

Nested loops allow printing stars row by row.

#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("*");
        }

        printf("\n");

    }

    return 0;

}

In this program, the outer loop controls the rows from 1 to the given number of rows. The inner loop prints stars equal to the current row number. After each row, a newline is printed to form the triangle.

Right-Angled Triangle

FAQs

Q1: Can we use a character other than *?
Yes, any symbol can replace * to create the triangle with a different character.

Q2: Can the triangle be inverted?
Yes, by reversing the outer loop and adjusting the inner loop, you can print an inverted right-angled triangle.

Q3: Can we align the triangle to the right?
Yes, by adding leading spaces in each row, you can print a right-aligned triangle.

Conclusion

This program demonstrates how to print a simple right-angled triangle using nested loops. By controlling the number of stars per row, a clear triangular pattern is formed. This logic can be extended to inverted triangles, hollow triangles, and other geometric patterns.

References & Additional Resources

Scroll to Top