打印空心倒置直角三角形星形图案的C++程序

71 阅读1分钟

编写一个C++程序,使用for循环打印空心倒置的右三角星形图案。

#include using namespace std;

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


cout << "Enter Hollow Inverted Right Triangle Star pattern Rows = ";
cin >> rows;

cout << "Hollow Inverted Right Angled Triangle Star Pattern\\n"; 

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

}

image.png

这个C++模式的例子使用while循环打印给定字符的空心倒直角三角形。

#include using namespace std;

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


cout << "Enter Rows = ";
cin >> rows;

cout << "Enter Symbol = ";
cin >> ch;

i = rows;
while( i > 0)
{
    j = 1;
	while( j <= i)
	{
        if(j == 1 || j == i || i == 1 || i == rows) 
        {
            cout << ch;
        }
        else
        {
            cout << " ";
        }   
        j++;      
    }
    cout << "\\n";
    i--;
}		
return 0;
Enter Rows = 15
Enter Symbol = $

$$$$$$$$$$$$$$$
$            $
$           $
$          $
$         $
$        $
$       $
$      $
$     $
$    $
$   $
$  $
$ $
$$
$