像字母三角形一样, 我们可以编写C ++程序来打印数字三角形。数字三角形可以用不同的方式打印。
让我们看一下打印数字三角形的C ++示例。
#include <iostream>
using namespace std;
int main()
{
int i, j, k, l, n;
cout<<"Enter the Range=";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
cout<<" ";
}
for(k=1;k<=i;k++)
{
cout<<k;
}
for(l=i-1;l>=1;l--)
{
cout<<l;
}
cout<<"\n";
}
return 0;
}
输出:
Enter the Range=5
1
121
12321
1234321
123454321
Enter the Range=6
1
121
2321
1234321
123454321
12345654321
评论前必须登录!
注册