pascal triangle c program code example

Example 1: program to print pascal triangle

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

Example 2: c programming print pattern pyramid

#include<stdio.h> 
int main() 
{
int i,j,n; 
printf("ENTER THE NUMBER OF ROWS YOU WANT TO PRINT * PYRAMID PATTERN\n"); 
scanf("%d", &n); 
for(i = 1 ; i <= n ; i++) 
{
    for (j = 1 ; j <= 2*n-1 ; j++) 
{
      if (j >= n-(i-1) && j <= n+(i-1)) 
      { printf("*"); }
      else 
      {printf(" "); } 
} 
printf("\n");
}

Tags:

C Example