Home > Backend Development > C++ > Write a program in C language that prints squares embedded in squares

Write a program in C language that prints squares embedded in squares

王林
Release: 2023-09-02 08:09:06
forward
1076 people have browsed it

Program Description

Print a square within another square as shown below

Write a program in C language that prints squares embedded in squares

Algorithm

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.
Copy after login
## The Chinese translation of #Example

is:

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;
}
Copy after login
Output

Write a program in C language that prints squares embedded in squares

The above is the detailed content of Write a program in C language that prints squares embedded in squares. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template