首頁  >  文章  >  後端開發  >  在C語言中編寫一個列印正方形內嵌正方形的程式

在C語言中編寫一個列印正方形內嵌正方形的程式

王林
王林轉載
2023-09-02 08:09:06995瀏覽

程式描述

依照下面所示的方式列印一個正方形內的另一個方塊

在C語言中編寫一個列印正方形內嵌正方形的程式

演算法

Accept the number of rows the outer Square to be drawn
Display the Outer Square with the number of rows specified by the User.
Display another square inside the outer square.

Example

的中文翻譯為:

範例

/* Program to print Square inside Square */
#include <stdio.h>
int main()
{
   int r, c, rows;
   clrscr();
   printf("Enter the Number of rows to draw Square inside a Square: ");
   scanf("%d", &rows);
   printf("</p><p>");
   for (r = 1; r <= rows; r++){
      for (c = 1; c <= rows; c++){
         if ((r == 1 || r == rows || c == 1 || c == rows) || (r >= 3 && r <= rows - 2 && c >= 3 && c             <= rows - 2) && (r == 3 || r == rows - 2 || c == 3 || c == rows - 2)){
               printf("#");
         }
         else{
            printf(" ");
         }
      }
      printf("</p><p>");
   }
   getch();
   return 0;
}

輸出

在C語言中編寫一個列印正方形內嵌正方形的程式

#

以上是在C語言中編寫一個列印正方形內嵌正方形的程式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除