Can this program be improved to print a hollow square? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Can this program be improved to print a hollow square?

I mean, can we improve the looping? https://code.sololearn.com/cJKUZr7XwDXo/?ref=app

13th Apr 2019, 3:25 AM
SWETA X
SWETA X - avatar
8 Answers
+ 22
No need to put character in an array, just use 2 loop, and inside those 2 loops, use Conditionals. Link to lesson: https://www.sololearn.com/learn/C/2923/?ref=app //use OR (||) between conditions as any of these conditions need to be true for placing star
13th Apr 2019, 4:22 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 20
● run 2 loops from 0 to n, and think it like a matrix in which you can use condition to put "*" for rowNumber=0, rowNumber=n, columnNumber=0, columnNumber=n else print a space " " ● moving to next line in each iteration of outer loop using "\n"
13th Apr 2019, 4:15 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 10
Gaurav Agrawal I had thought of it, but how do I put a character in an array? Should I use 'putchar' ? If yes, how?
13th Apr 2019, 4:18 AM
SWETA X
SWETA X - avatar
+ 9
YUGABDH PASHTE I mean, to print a solid square, we use a single nested loop like this..... https://code.sololearn.com/cV8mgnszagkm/?ref=app Can we use a similar loop for a hollow square?
13th Apr 2019, 4:14 AM
SWETA X
SWETA X - avatar
+ 3
https://code.sololearn.com/cj24jL6h3etd/?ref=app Try this one you have to give len of side of square
13th Apr 2019, 4:42 AM
Yugabdh
Yugabdh - avatar
+ 1
Isn't it's like your code already doing it? What else you want to do with it?
13th Apr 2019, 4:08 AM
Yugabdh
Yugabdh - avatar
0
What is "return 0" used for? Code runs even without it.
14th Apr 2019, 1:16 PM
D. Hussaini
D. Hussaini - avatar
0
It returns 0 to calling function As in my code I have return type as void so it's working even if you don't write return 0. When you have return type as int u suppose to write return 0 As calling function expecting int as return value.
14th Apr 2019, 1:44 PM
Yugabdh
Yugabdh - avatar