Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: Pascaltriangle.c
- Copyright:
- Author: Mr.Kindle
- Date: 30-11-22 21:44
- Description: This code print the pascal triangle of given number of rows.
- */
- #include<stdio.h>
- int ncr (int, int);
- int factorial(int);
- int main()
- {
- int n,i,j;
- printf("Enter number of rows: ");
- scanf("%d",&n);
- /*now making pascal triangle*/
- for(i=0;i<n;i++)
- { /*for printing space*/
- for(j=0;j<n-i;j++)
- printf(" ");
- /*for printing numbers*/
- /*if we consider pascal triangle as a matrix then each number printed is nCr of corresponding 'i'
- and 'j'. where 'i' is the corresponding row index and 'j' is the corresponding column index*/
- for(j=0;j<=i;j++)
- printf("%d ",ncr(i,j));
- printf("\n");
- }
- return 0;
- }//main
- int ncr (int i, int j)
- {
- int num;
- num = factorial(i)/(factorial(i-j) * factorial(j));
- return num;
- }//end of ncr
- int factorial(int n)
- {
- int prod=1,i;
- for(i=1;i<=n;i++)
- prod = prod * i;
- return prod;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement