How to code rectangle inside the given rectangle in c | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to code rectangle inside the given rectangle in c

********** * * * * * * * * ********** I tried this code #include <stdio.h> int main() { int a=10, b=6; for(int i=1; i<=b; i++) { for(int j=1; j<=a; j++) { if(i==1||i==6||j==1||j==10) printf("*"); else{ printf(" "); } } printf("\n"); } return 0; }

20th Oct 2019, 9:26 PM
Alfred Monir
Alfred Monir - avatar
10 Answers
20th Oct 2019, 10:11 PM
Sonic
Sonic - avatar
+ 4
If you want to use the same approach, use more conditions using i and j to draw the inner rectangle.
20th Oct 2019, 9:44 PM
Sonic
Sonic - avatar
+ 4
Alfred Monir Hugo no problem. I gave the answer because you showed your initial code and made an attempt asking reasonable questions. Sometimes people ask for answers without making any attempt. For these questions, we're discouraged from showing a solution immediately. Hope you can take it from here for similar problems.
20th Oct 2019, 10:17 PM
Sonic
Sonic - avatar
+ 3
BTW if what you have drawn is the outer rectangle, you don't have enough 'rows' to have a border between the two rectangles and to also have any vertical space inside the inner one.
20th Oct 2019, 9:46 PM
Sonic
Sonic - avatar
+ 3
You don't have to.
20th Oct 2019, 9:49 PM
Sonic
Sonic - avatar
+ 3
If you are using console output, it is not possible to draw one rectangle first and then the other one because there is no easy way to reposition the cursor.
20th Oct 2019, 10:13 PM
Sonic
Sonic - avatar
+ 2
Sonic ok I am trying on that but output is getting different
20th Oct 2019, 9:45 PM
Alfred Monir
Alfred Monir - avatar
+ 2
Sonic yes it is the outer rectangle, so should we draw inner rectangle first?
20th Oct 2019, 9:48 PM
Alfred Monir
Alfred Monir - avatar
+ 2
Sonic then what is the correct way to get the output
20th Oct 2019, 9:50 PM
Alfred Monir
Alfred Monir - avatar
+ 1
Sonic Thank you for the answer
20th Oct 2019, 10:13 PM
Alfred Monir
Alfred Monir - avatar