Quantcast
Channel: Java Question Bank » Cpp Programs
Viewing all articles
Browse latest Browse all 22

A CPP Program to generate Pascal Triangle

$
0
0

A CPP Program to generate Pascal Triangle

============================================================================================

program :To generate pascal triangle

Source Code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
int i,j,k,n;
clrscr();
cout<<"Enter no. of rows:  ";
cin>>n;
cout<<"The pascal triangle is:"<<endl;
for(i=0;i<n;i++)
{
for(j=i;j<n-1;j++)
cout<<"  ";
int binom=1;
for(k=0;k<=i;k++)
{
if(i==0||k==0)
cout<<"   "<<binom;
else
{
binom=binom*(i-k+1)/k;
cout<<"   "<<binom;
}
}
cout<<"\n\n";
}
getch();
}

/* Output:
Enter no. of rows:  5
The pascal triangle is:

1
2
3
4
5
6
7
8
9
1

1   1

1   2   1

1   3   3   1

1   4   6   4   1

     */

 

82 total views, no views today


Viewing all articles
Browse latest Browse all 22

Trending Articles