Print Right Triangle Star Pattern

PRINTING PATTERN:

*

**

***

****

PREREQUISITE:

Basic knowledge of C language and use of loops.

ALGORITHM:

  1. Take the number of rows as input from the user  and store it in any variable.(‘r‘ in this case).
  2. Run a loop ‘r’ number of times to iterate through each of the rows. From i=0 to i<r. The loop should be structured as for( i=0 ; i<r : i++).
  3.  Run a nested loop inside the main loop to print the stars in each row of the triangle. From j=0 to j<i. The loop should be structured as for( j=0; j<=i ; j++).
  4. In the nested loop print star.
  5. In the main loop print a new line.

Code in C:

#include<stdio.h>
int main()
{
int i,j,r;      //declaring integer variables i,j for loops and r for number of rows
printf("Enter the number of rows :\n");     //Asking user for input
scanf("%d",&r);     //taking number of rows and saving it in variable r
for(i=0;i<r;i++)     // loop for number of rows
   {
      for(j=0;j<=i;j++)      // loop for stars per each row
      {
         printf("*");       //printing stars
      }
      printf("\n");         // printing newline after each row
   }
}

TAKING INPUT:
DISPLAYING OUTPUT:

10 comments on “Print Right Triangle Star Pattern”


  • chat.soumadeep

    //Code in JavaScript
    let row=4;
    let col=4;
    let i,j,k;

    for(i=row;i>0;i–)
    {
    let str=””;
    for(j=row;j>i-1;j–)
    {
    str=str+”*”;
    }
    console.log(str);
    }


  • Lalit Kr.

    Code in JAVA:
    import java.util.*;
    public class Star5 {
    public static void main(String arg[])
    {
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    for(int i=0; i<=n; i++)
    {
    for(int j=0; j<i; j++)
    {
    System.out.print("*");
    }
    System.out.println();
    }
    }
    }


  • Bindhu

    n=int(input())
    for i in range(n+1):
    print(‘*’*i,end=” “)
    print(“\n”)


  • Rekha Reddy

    n=int(input())
    for i in range(0,n):
    for j in range(0,n):
    if(j<=i):
    print('*',end=' ')
    else:
    print(' ',end=' ')
    print()


  • Nishitha Reddy

    Code in python
    X=int(input(“enter any digit”))
    For I in range(0, x) :
    For j in range(0, x) :
    If(I<j) :
    Print(" ", end=" ")
    Else:
    Print("*", end=" ")
    Print()