Pages

Friday, November 29, 2019

Triangle Star Pattern


Following C program will print a triangle pattern based on given number N. For example, for n=3, program will print

   * 
  * * 
 * * * 

Triangle Star Pattern C Program


#include<stdio.h>

int main()
{
  int i, j, n;
  printf("Enter your triangle pattern size:");
  scanf("%d", &n);
  
  for(i=0; i<=n; i++)
  {
    for(j=0; j<=n-1-i; j++)
       printf(" "); 
    for(j=0; j<i; j++)
       printf("* "); 
    printf("\n"); 
  }
  return 0;
}

Program output for Execution 1:

Enter your triangle pattern size:3       
   * 
  * * 
 * * * 

Program output for Execution 2:

Enter your triangle pattern size:4       
     * 
    * * 
   * * * 
  * * * * 

Program output for Execution 3:

Enter your triangle pattern size:7       
      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 
* * * * * * * 

Click here to read more interesting C Patterns Practicals.