Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Sunday 19 August 2018

C program to print pascal triangle

#include<stdio.h>
long factorial(int);
Pascal Triangle

int main()
{
    int i, n, c;
   printf("How many rows you want in pascal triangle?\n");
       scanf("%d",&n);
     for ( i = 0 ; i < n ; i++ )
    {
        for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
        printf(" ");
        for( c = 0 ; c <= i ; c++ )
            printf("%ld",factorial(i)/(factorial(c)*factorial(i-c)));
            printf("\n");
    }
    return 0;
}

long factorial(int n)
{
    int c;    
    long result = 1;
    
    for( c = 1 ; c <= n ; c++ )
    result = result*c;
    return ( result );
}

No comments:

Post a Comment