Probably this is one of the toughest pattern to print using
C program for beginners , but after going through this post this won't be tough
any more.
Lets focus on definition , what pascal triangle comprises
of...
In mathematics, Pascal's triangle is a triangular array of
the binomial coefficients.
Now , see .
Now , probably you are getting the point i am talking about.
We can simply print the pattern by calling a function of
*combination.
The two for loops are required , for row and column and
using the following logic we can print the pascal triangle up to required
number of rows .
C program : Printing a Pascal's Triangle
#include < stdio.h > int comb();/*function for finding combination*/ int main() } } int fact();/*functions for finding factorial*/ { int i,j,n; int ans; printf("Enter number of rows of pascal's triangle :\n"); scanf("%d",&n); for(i=0;i < =n;i++)/*main for loop*/ { for(j=n-i;j!=0;j--)/*nested for loop for managing space*/ { printf(" "); } for(j=0;j < =i;j++)/*nested for loop for iCj=nCr */ { ans=comb(i,j);/*calling a function for combination*/ printf("%d ",ans);/*printing the required number*/ } printf("\n");/*printing new line after dealing with one row*/ int comb(int x,int y) { int a,b,c,d; a=fact(x);/*calling a function for factorial*/ b=fact(y); c=fact(x-y); d=a/(b*c); return(d); } int fact(int x) { int i,p=1; for(i=1;i < =x;i++) { p=p*i; } return(p); }
*For explanation of functions of combination and factorial
refer to the link below :
Permutation and Combination using C functions !
Permutation and Combination using C functions !
Thank you guys for visiting !
Suggestions and feedback are always accepted .
Awesome buddy.... Just focus on large number.. Eg if two digit number appears in your triangle it disturb you triangle pattern.. Otherwise it will perfect 👌..
ReplyDeleteya , I know but
DeleteI was unable to resolve that problem because computer can't adjust space for more digits at single space.
I would love to know if it is possible ..
thanks you!