C++ 打印空心框数字图案程序

用 For 循环编写一个 C++ 程序来打印空心框数字图案,并附带示例。在此示例中,我们在嵌套的 for 循环中使用 if 语句来检查框的边界。如果是框的边界,则打印 1;否则,打印空格。

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
     
    cout << "\nPlease Enter the Number of Rows = ";
    cin >> rows;
    
    cout << "\nPlease Enter the Number of Columns = ";
    cin >> columns;
     
    cout << "\n---Hallow Box Number Pattern-----\n";
    for(i = 1; i <= rows; i++)
    {
    	for(j = 1; j <= columns; j++)
		{
			if(i == 1 || i == rows || j == 1 || j == columns)
			{
				cout << "1";
			}
			else
			{
				cout << " ";
			}       	
        }
        cout << "\n";
    }
		
 	return 0;
}
C++ Program to Print Hollow Box Number Pattern

C++ 打印零的空心框图案程序

在这里,我们将 1 替换为 0。

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
     
    cout << "\nPlease Enter the Number of Rows = ";
    cin >> rows;
    
    cout << "\nPlease Enter the Number of Columns = ";
    cin >> columns;
     
    cout << "\n---Hallow Box Number Pattern-----\n";
    for(i = 1; i <= rows; i++)
    {
    	for(j = 1; j <= columns; j++)
		{
			if(i == 1 || i == rows || j == 1 || j == columns)
			{
				cout << "0";
			}
			else
			{
				cout << " ";
			}       	
        }
        cout << "\n";
    }
		
 	return 0;
}
Please Enter the Number of Rows = 7

Please Enter the Number of Columns = 15

---Hallow Box Number Pattern-----
000000000000000
0             0
0             0
0             0
0             0
0             0
000000000000000

此程序将使用 While 循环打印空心框数字图案。

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns;
     
    cout << "\nPlease Enter the Number of Rows = ";
    cin >> rows;
    
    cout << "\nPlease Enter the Number of Columns = ";
    cin >> columns;
     
    cout << "\n---Hollow Box Number Pattern-----\n";
    i = 1; 
    while(i <= rows)
    {
    	j = 1; 
    	while(j <= columns)
		{
			if(i == 1 || i == rows || j == 1 || j == columns)
			{
				cout << "1";
			}
			else
			{
				cout << " ";
			}     
			j++;  	
        }
        cout << "\n";
        i++;
    }
		
 	return 0;
}
Please Enter the Number of Rows = 7

Please Enter the Number of Columns = 16

---Hollow Box Number Pattern-----
1111111111111111
1              1
1              1
1              1
1              1
1              1
1111111111111111

在此 C++ 空心框数字图案示例中,我们允许用户输入自己的数字来打印边框。

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, num;
     
    cout << "\nPlease Enter the Number of Rows = ";
    cin >> rows;
    
    cout << "\nPlease Enter the Number of Columns = ";
    cin >> columns;
    
    cout << "\nPlease Enter Any Integer Value = ";
    cin >> num;
     
    cout << "\n---Hallow Box Number Pattern-----\n";
    for(i = 1; i <= rows; i++)
    {
    	for(j = 1; j <= columns; j++)
		{
			if(i == 1 || i == rows || j == 1 || j == columns)
			{
				cout << num;
			}
			else
			{
				cout << " ";
			}       	
        }
        cout << "\n";
    }
		
 	return 0;
}
Please Enter the Number of Rows = 12

Please Enter the Number of Columns = 25

Please Enter Any Integer Value = 9

---Hallow Box Number Pattern-----
9999999999999999999999999
9                       9
9                       9
9                       9
9                       9
9                       9
9                       9
9                       9
9                       9
9                       9
9                       9
9999999999999999999999999