卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章64334本站已运行4115

C语言中打印实心和空心正方形图案的程序

程序描述

在几何学中,正方形是一种正规四边形,意味着它有四条相等的边和四个相等的角。

实心和空心正方形将如下所示

C语言中打印实心和空心正方形图案的程序

算法

对于实心正方形 -

'
Accept the Number of Rows from the user to draw the Solid Square
For each Row, Print * for each Column to draw the Solid Square

对于空心正方形 −

'
Accept the Number of Rows from the user to draw the Hollow Square
For the First and Last Row, Print * for each Column
For the Remaining Rows, Print * for the first and Last Column.

Example

的中文翻译为:

示例

'
/* Program to print hollow and Solid Square pattern */
#include <stdio.h>
int main(){
   int r, c, rows; //Hollow Rhombus
   int r1,c1, rows1; //Solid Rhombus
   clrscr();
   /* Hollow Square */
   printf("Enter the Number of rows for Hollow Square: ");
   scanf("%d", &rows);
   printf("<p>");
   for (r=1; r<=rows; r++){
      if (r==1 || r==rows){
         for (c=1; c<=rows; c++){
            printf("*");
         }
      }
      else{
         for (c=1; c<=rows; c++){
            if (c==1 || c==rows){
               printf("*");
            }
            else{
               printf(" ");
            }
         }
      }
      printf("</p><p>");
   }
   printf("</p><p>");
   /* Solid Square */
   printf("Enter the Number of rows for Solid Square: ");
   scanf("%d", &rows1);
   printf("</p><p>");
   for (r1=1; r1<=rows1; r1++){
      for (c1=1; c1<=rows1; c1++){
         printf("*");
      }
      printf("</p><p>");
   }
   getch();
   return 0;
}</p>

输出

C语言中打印实心和空心正方形图案的程序

卓越飞翔博客
上一篇: 计算要与频率大于其他字符频率之和的字符连接的字符串数量
下一篇: 在C语言中的格式说明符
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏