![](https://i-blog.csdnimg.cn/blog_migrate/666b51b97ed99c074b7c19e100687d6f.png)
c++转pascal程序
Here I have shared simple program for pascal triangle in C and C++.
在这里,我共享了C和C ++中用于pascal三角形的简单程序。
Basically Pascal’s triangle is a triangular array of binomial coefficients. An example for how pascal triangle is generated is illustrated in below image. If you have any doubts then you can ask it in comment section.
基本上,帕斯卡的三角形是二项式系数的三角形阵列。 下图显示了如何生成Pascal三角形的示例。 如果您有任何疑问,可以在评论部分提出。
Each number is the sum of the two directly above it.
每个数字都是其正上方的两个数字之和。
![Pascal Triangle Animated](https://i-blog.csdnimg.cn/blog_migrate/727d96f46e32403ce4a6de2f03dff9b4.png)
C语言中Pascal Triangle的程序 (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;
}
C ++中的Pascal Triangle程序 (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
输出量
![Program for Pascal Triangle in C and C++](https://i-blog.csdnimg.cn/blog_migrate/427418c483aed6958dcf653d6ac0e187.png)
翻译自: https://www.thecrazyprogrammer.com/2015/11/program-for-pascal-triangle-in-c-and-c.html
c++转pascal程序