To
Print Pascal Triangle in C
#include<stdio.h>
#include<conio.h>
long fact(int m); //function
declaration
long fact(int m) //
function definition
{
int c;
// c is counter variable
long result = 1;
for( c = 1 ; c <= m ; c++ )
result
= result*c;
return
result;
}
void main()
{
Int i, n, c;
clrscr();
printf("How many rows you want to
enter 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
",fact(i)/(fact(c)*fact(i-c)));
printf("\n");
}
getch();
}
OUTPUT:
How many rows you want to enter in pascal triangle?
5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Note:
Pascal's triangle is a set
of numbers arranged in the form of a triangle. Each number in a row is the sum
of the left number and right number on the above row. If a number is missing in
the above row, it is assumed to be 0. The first row starts with number 1.
No comments:
Post a Comment