C++程序打印倒置的直角三角形字母图案

69 阅读1分钟

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

#include using namespace std;

int main() { int rows;


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

cout << "Printing Inverted Right Triangle Alphabets Pattern\\n";
int alphabet = 65;

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

image.png

#include using namespace std;

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


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

cout << "Printing Inverted Right Triangle Alphabets Pattern\\n";
alphabet = 65;

i = rows - 1;
while (i >= 0)
{
	j = 0;
	while (j <= i)
	{
		cout << char(alphabet + j);
		j++;
	}
	cout << "\\n";
	i--;
}
Enter Inverted Right Triangle of Alphabets Rows = 14
Printing Inverted Right Triangle Alphabets Pattern
ABCDEFGHIJKLMN
ABCDEFGHIJKLM
ABCDEFGHIJKL
ABCDEFGHIJK
ABCDEFGHIJ
ABCDEFGHI
ABCDEFGH
ABCDEFG
ABCDEF
ABCDE
ABCD
ABC
AB
A

这个C++例子使用do while循环打印字母的倒直角三角形图案。

#include using namespace std;

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

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

cout << "Printing Inverted Right Triangle Alphabets Pattern\\n";
alphabet = 65;

i = rows - 1;
do
{
	j = 0;
	do
	{
		cout << char(alphabet + j);

	} while (++j <= i);
	cout << "\\n";

} while (--i >= 0);
Enter Inverted Right Triangle of Alphabets Rows = 17
Printing Inverted Right Triangle Alphabets Pattern
ABCDEFGHIJKLMNOPQ
ABCDEFGHIJKLMNOP
ABCDEFGHIJKLMNO
ABCDEFGHIJKLMN
ABCDEFGHIJKLM
ABCDEFGHIJKL
ABCDEFGHIJK
ABCDEFGHIJ
ABCDEFGHI
ABCDEFGH
ABCDEFG
ABCDEF
ABCDE
ABCD
ABC
AB
A