C Program to Print Number Star Right Triangle Pattern Type4

Number Star Right Triangle Pattern

Today we will learn how to print pattern using C programming language with the help of basic knowledge of C and usage of loops.
To print patterns of numbers and stars (*) , we need to use two loops, first is outer loop and the second is inner loop. Where outer loop is responsible for print rows and the inner loop is responsible for print columns.

C Program to Print Number Star Right Triangle Pattern

     Algorithm:-

  1. Take input from user i.e number of lines required (N value).
  2. Take a result variable (say ‘a’) and initialize it with 1.
  3. Take two loops one for each line (say ‘i’) and other for each digit in a particular line (say ‘j’).
  4. Here ‘i’ loop is used to access each line from 1 to N and ‘j’ loop is used to print values in each line. For example in line 2 the value of i=2 and for contents in second line (i.e 3*2) the value for j for digit 3 is j=1 and for digit 2 is j=2.
  5. Increment the ‘a’ value at the beginning of each line to N values by using (i*(i+1))/2 since we need to print digits in reverse order.
  6. For example, When control is in line 2, since i=2, the corresponding ‘a’ value in the loop becomes (2*(2+1))/2=3. Hence 3* is printed for j=1 and is decremented since we used post decrement and for j=2 the digit 2 is printed.
  7. Repeat the ‘i’ loop until it reaches ‘N’ lines.

C Code:-

// C Program to print Number Star Right Triangle Pattern
#include <stdio.h>          
void main()                 
    {
        int N,i,j,a=1;        
        printf("Enter the N value (number of lines):");     
        scanf("%d",&N);//receiving the input from user using scanf()
        for(i=1;i<=N;i++) //Starting of i loop indicating number of lines in the output from 1 to N
            {    
                a=(i*(i+1))/2;//skipping the value of a by N value, as the digits need to be printed in -reverse
                for(j=1;j<i;j++)
                  {     
                    printf("%d*",a--);//printing the value in a(except the last digit
                                    //in each line  with * and post-decrementing it
                  }
                printf("%d\n",a--);   
                
            }
    }