C++ 打印数字三角形基础教程文档

收录于 2023-04-20 00:10:05 · بالعربية · English · Español · हिंदीName · 日本語 · Русский язык · 中文繁體

类似于字母三角形,我们可以编写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