top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C program to print a pascals triangle?

+3 votes
325 views

This is pascals triangle -

Pascal Triangle

Now the task is to write a program which print this output in C?

posted Oct 6, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
#include<stdio.h>

long fact(int);
int main(){
    int line,i,j;

    printf("Enter the no. of lines: ");
    scanf("%d",&line);

    for(i=0;i<line;i++){
         for(j=0;j<line-i-1;j++)
             printf(" ");

         for(j=0;j<=i;j++)
             printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
         printf("\n");
    }
    return 0;
}

long fact(int num){
    long f=1;
    int i=1;
    while(i<=num){
         f=f*i;
         i++;
  }
  return f;
 }

Sample output:

Enter the no. of lines: 8
       1
      1 1
     1 2 1
    1 3 3 1
   1 4 6 4 1
  1 5 10 10 5 1
 1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
answer Dec 1, 2014 by Shivaranjini
Similar Questions
–1 vote

Floyd's triangle is a right-angled triangular array of natural numbers, used in computer science education. It is named after Robert Floyd. It is defined by filling the rows of the triangle with consecutive numbers, starting with a 1 in the top left corner.
Please help me how should i print the following pattern using c language.

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

...