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

271 阅读1分钟

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

#include using namespace std;

int main() { int rows;


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

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

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

image.png

#include using namespace std;

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

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

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

i = 0;

while (i <= rows - 1)
{

	j = 0;
	while (j <= i)
	{
		cout << " ";
		j++;
	}

	k = 0;
	while (k <= rows - i - 1)
	{
		cout << char(alphabet + k) << " ";
		k++;
	}
	cout << "\\n";
	i++;
}
Enter Inverted Triangle of Alphabets Rows = 15
Inverted Triangle Alphabets Pattern
 A B C D E F G H I J K L M N O 
  A B C D E F G H I J K L M N 
   A B C D E F G H I J K L M 
    A B C D E F G H I J K L 
     A B C D E F G H I J K 
      A B C D E F G H I J 
       A B C D E F G H I 
        A B C D E F G H 
         A B C D E F G 
          A B C D E F 
           A B C D E 
            A B C D 
             A B C 
              A B 
               A 

这个C++例子使用do while循环显示字母的倒三角模式。

#include using namespace std;

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

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

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

i = 0;

do
{
	j = 0;
	do
	{
		cout << " ";

	} while (j++ <= i);

	k = 0;
	do
	{
		cout << char(alphabet + k) << " ";

	} while (++k <= rows - i - 1);

	cout << "\\n";

} while (++i <= rows - 1);
Enter Inverted Triangle of Alphabets Rows = 17
Inverted Triangle Alphabets Pattern
  A B C D E F G H I J K L M N O P Q 
   A B C D E F G H I J K L M N O P 
    A B C D E F G H I J K L M N O 
     A B C D E F G H I J K L M N 
      A B C D E F G H I J K L M 
       A B C D E F G H I J K L 
        A B C D E F G H I J K 
         A B C D E F G H I J 
          A B C D E F G H I 
           A B C D E F G H 
            A B C D E F G 
             A B C D E F 
              A B C D E 
               A B C D 
                A B C 
                 A B 
                  A