使用 for 循环编写一个 C++ 程序,打印连续字母组成的直角三角形图案。
#include<iostream>
using namespace std;
int main()
{
int rows;
cout << "Enter Right Triangle of Consecutive Alphabets Rows = ";
cin >> rows;
cout << "Right Triangle of Consecutive Alphabets Pattern\n";
int alphabet = 65;
for (int i = 0; i <= rows - 1; i++)
{
for (int j = 0; j <= i; j++)
{
cout << char(alphabet++) << " ";
}
cout << "\n";
}
}

使用 while 循环打印连续字母组成的直角三角形图案的 C++ 程序。
#include<iostream>
using namespace std;
int main()
{
int rows, i, j, alphabet;
cout << "Enter Right Triangle of Consecutive Alphabets Rows = ";
cin >> rows;
cout << "Right Triangle of Consecutive Alphabets Pattern\n";
alphabet = 65;
i = 0;
while (i <= rows - 1)
{
j = 0;
while (j <= i)
{
cout << char(alphabet++) << " ";
j++;
}
cout << "\n";
i++;
}
}
Enter Right Triangle of Consecutive Alphabets Rows = 9
Right Triangle of Consecutive Alphabets Pattern
A
B C
D E F
G H I J
K L M N O
P Q R S T U
V W X Y Z [ \
] ^ _ ` a b c d
e f g h i j k l m
此 C++ 示例 使用 do while 循环在每一列中显示连续字母组成的直角三角形图案。
#include<iostream>
using namespace std;
int main()
{
int rows, i, j, alphabet;
cout << "Enter Right Triangle of Consecutive Alphabets Rows = ";
cin >> rows;
cout << "Right Triangle of Consecutive Alphabets Pattern\n";
alphabet = 65;
i = 0;
do
{
j = 0;
do
{
cout << char(alphabet++) << " ";
} while (++j <= i);
cout << "\n";
} while (++i <= rows - 1);
}
Enter Right Triangle of Consecutive Alphabets Rows = 10
Right Triangle of Consecutive Alphabets Pattern
A
B C
D E F
G H I J
K L M N O
P Q R S T U
V W X Y Z [ \
] ^ _ ` a b c d
e f g h i j k l m
n o p q r s t u v w