C++程序打印同列数字的倒置的直角三角形

321 阅读1分钟

编写一个C++程序,使用for循环打印同一列数字的倒置的直角三角形。

#include using namespace std;

int main() { int i, j, rows;

cout << "Enter Inverted Right Triangle of Numbers Rows = ";
cin >> rows;

cout << "Inverted Right Triangle of Same Column Numbers Pattern\\n";  

for(i = rows; i >= 1; i--)
{
	for(j = 1; j <= i; j++)
	{
        cout << i << " ";
    }
    cout << "\\n";
}		
return 0;

image.png

这个C++例子使用while循环打印相同列数的倒置的直角三角形。

#include using namespace std;

int main() { int i, j, rows;

cout << "Enter Inverted Right Triangle of Numbers Rows = ";
cin >> rows;

cout << "Inverted Right Triangle of Same Column Numbers Pattern\\n";

i = rows;

while( i >= 1)
{
    j = 1;
	while( j <= i)
	{
        cout << i << " ";
        j++;
    }
    cout << "\\n";
    i--;
}		
return 0;
Enter Inverted Right Triangle of Numbers Rows = 8
Inverted Right Triangle of Same Column Numbers Pattern
8 8 8 8 8 8 8 8 
7 7 7 7 7 7 7 
6 6 6 6 6 6 
5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1