C program to print right triangle star pattern

Write a C program to print right triangle star(*) pattern series using for loop. How to print right triangle star pattern series of n rows using for loop in C programming. The pattern with 5 rows should look like:
*
**
***
****
*****

Required knowledge:

Basic C programming, For loop

Logic:

Printing right triangle pattern is simple if you got the pattern. If you look to the pattern carefully you will find that you have to print stars in increasing order of rows (i.e. 1 star in first row, followed by 2 stars in second and so on...). To do so we will use the concept of square star pattern with a little change in inner loop code. In the pattern given above the total number of stars(columns) in each row is equal to the row number and we will use this as a termination limit in the inner for loop.

Program:

/*
 * C program to print right triangle star pattern series
 */

#include <stdio.h>

int main()
{
    int i, j, n;

    //Reads the number of rows to be printed from user
    printf("Enter value of n: ");
    scanf("%d", &n);

    for(i=1; i<=n; i++)
    {
        //Print i number of stars
        for(j=1; j<=i; j++)
        {
            printf("*");
        }

        //Move to next line/row
        printf("\n");
    }

    return 0;
} 


Output
X
_
Enter the value of n: 5
*
**
***
****
*****


Screenshot:

Right triangle star pattern program in C

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment