c++转pascal程序_C和C ++中的Pascal Triangle程序

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

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://www.thecrazyprogrammer.com/2015/11/program-for-pascal-triangle-in-c-and-c.html

c++转pascal程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值