Program to Print Pascal Triangle

该博客介绍了用 C++ 打印帕斯卡三角形的方法。程序会让用户输入要打印的行数,通过三个 for 循环实现打印功能,并给出了完整的 C++ 代码示例,包含主函数和计算阶乘的函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}

转载于:https://www.cnblogs.com/poission/p/10905531.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值