Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Array

Patterns

Excersises


Print Triangle Border Pattern Using For Loop In C Program


Program to print triangle border pattern of n rows using for loop.

Example : pgm.c

#include<stdio.h>
int main(){
    int i,j,n;
	printf("\n Enter the limit:");
	scanf("%d",&n);
    for(i=1;i<=n;i++){
        for(j=1;j<=n-i;j++){
            printf(" ");
        }
        for(j=1;j<=i;j++){
            if(i==1||i==n||j==1){
                printf("*");
            }else{
                printf(" ");
            }
        }
        for(j=1;j<i;j++){
             if(i==1||i==n||j==i-1){
                printf("*");
            }else{
                printf(" ");
            }
        }
        printf("\n");
    }
    return 0;
}

Output:

Enter the limit: 10
         *
        * *
       *   *
      *     *
     *       *
    *         *
   *           *
  *             *
 *               *
*******************