Tuesday, March 1, 2016

Pascal Triangle in C

1:  #include<stdio.h>  
2:    
3:  long fact(int);  
4:  int main(){  
5:    int n,i,j;  
6:    
7:    printf("Enter the no. of lines: ");  
8:    scanf("%d",&n);  
9:    
10:    for(i=0;i<n;i++){  
11:       for(j=0;j<n-i-1;j++)  
12:         printf(" ");  
13:    
14:       for(j=0;j<=i;j++)  
15:         printf("%ld ",fact(i)/(fact(j)*fact(i-j)));  
16:       printf("\n");  
17:    }  
18:    return 0;  
19:  }  
20:    
21:  long fact(int num){  
22:    long f=1;  
23:    int i=1;  
24:    while(i<=num){  
25:       f=f*i;  
26:       i++;  
27:   }  
28:   return f;  
29:   }  
30:    

No comments:

Post a Comment

Thank you for commenting. Please wait for response :)