Here I have shared straightforward program for pascal triangle in C and C++.
Fundamentally Pascal’s triangle is a triangular exhibit of binomial coefficients. A model for how the pascal triangle is produced is represented in beneath picture. On the off chance that you have any questions, at that point, you can ask it in the remark segment.
Each number is the entirety of the two legitimately above it.
Program for Pascal Triangle in C
#include<stdio.h>
//function to calculate factorial
long fact(int x)
{
int i;
long f=1;
for(i=1;i<=x;++i)
{
f=f*i;
}
return f;
}
int main()
{
int i,j,k,n;
printf("How many lines? ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
//loop to print spaces at starting of each row
for(j=1;j<=(n-i-1);++j)
{
printf(" ");
}
//loop to calculate each value in a row and print it
for(k=0;k<=i;++k)
{
printf("%ld ",fact(i)/(fact(i-k)*fact(k)));
}
printf("\n"); //print new line after each row
}
return 0;
}
Program for Pascal Triangle in C++
#include<iostream>
using namespace std;
//function to calculate factorial
long fact(int x)
{
int i;
long f=1;
for(i=1;i<=x;++i)
{
f=f*i;
}
return f;
}
int main()
{
int i,j,k,n;
cout<<"How many lines? ";
cin>>n;
for(i=0;i<n;++i)
{
//loop to print spaces at starting of each row
for(j=1;j<=(n-i-1);++j)
{
cout<<" ";
}
//loop to calculate each value in a row and print it
for(k=0;k<=i;++k)
{
cout<<fact(i)/(fact(i-k)*fact(k))<<" ";
}
cout<<"\n"; //print new line after each row
}
return 0;
}
Output