首页 > 其他 > 详细

Program to Print Pascal Triangle

时间:2019-05-22 14:20:22      阅读:101      评论:0      收藏:0      [点我收藏+]

Print Pascal Triangle in C++

To print pascal triangle in C++ programming, you have to ask to the user to enter the number of line (upto which he/she want to print pascal triangle).

So to print pascal triangle, you have to use three for loops as shown here in the following program.

C++ Programming Code to Print Pascal Triangle

Following C++ program ask to the user to enter number of line or row upto which the Pascal triangle

will be printed to print the Pascal triangle and display it on the screen:

 

#include <iostream>

using namespace std;

int main()
{
//
int i, n, c;
//
long fact(int n);
cout<<"Upto how many line (Enter number of rows) : ";
cin>>n;
//
for(i=0; i<n; i++)
{
//
for(c=0; c<=(n-i-2); c++)
{
cout<<" ";
}
//
for(c=0; c<=i; c++)
{
cout<<fact(i)/(fact(c)*fact(i-c));
cout<<" ";
}
cout<<"\n";
}
return 0;
}

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

return 0;

}

Program to Print Pascal Triangle

原文:https://www.cnblogs.com/poission/p/10905531.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!