A C program to display a table that represents a Pascal triangle of any size – IGNOU MCA Assignment 2014 – 15

By | July 15, 2014

MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-011
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/011/Assign/2014-15
Maximum Marks : 100
Weightage : 25%

Write an interactive C program to display a table that represents a Pascal triangle of any size.

Hint: In Pascal triangle, the first and the second rows are set to 1. Each element of the triangle (from the third row downward) is the sum of the element directly above it and the element to the left of the element directly above it. See the below given example Pascal Triangle of size=5:

#include<stdio.h>
long fact(int);
void main()
{
int i,j,num,n;
clrscr();
printf(“Enter number of rows in pascal triangle : “);
scanf(“%d”,&num);
for(i=0;i<num;i++)
{
for(j=0;j<=i;j++)
{
n=fact(i)/(fact(j)*fact(i-j));
printf(“%d “,n);
}
printf(“\n”);
}
getch();
}

long fact(int n)
{
int x;
long res=1;
for(x=1;x<=n;x++)
res=res*x;
return(res);
}

Code:

 Source code
```#include<stdio.h>
long fact(int);
void main()
{
int i,j,num,n;
clrscr();
printf("Enter number of rows in pascal triangle : ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
for(j=0;j<=i;j++)
{
n=fact(i)/(fact(j)*fact(i-j));
printf("%d ",n);
}
printf("\n");
}
getch();
}
long fact(int n)
{
int x;
long res=1;
for(x=1;x<=n;x++)
res=res*x;
return(res);
}```

Screen Shots: