首页  >  文章  >  后端开发  >  在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删除