Square | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Square

how to bring out such a figure? * * * * * * * * * * https://code.sololearn.com/cGk5OAjyA3Z4/?ref=app

9th Jun 2021, 1:22 PM
Best of Sport
Best of Sport - avatar
12 Answers
+ 2
Try it in same way as you did it for x. Try it and post your attempt,if you find difficulty... edit: Best of Sport if this code not your implementation then observe it 'where spaces adding and where printing * '. you will get similar examples here only.. so try it and find some logic, if that does not work then use this thread for help.. some one definitely help. happy learning...
9th Jun 2021, 1:38 PM
Jayakrishna 🇮🇳
+ 1
Compare when j==0 and j==n-1 print * else space
9th Jun 2021, 2:07 PM
Atul [Inactive]
+ 1
Best of Sport No, not like that public class Program { public static void main(String[] args) { int n=7; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if((j==(1) || (j==n-1))) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } } //This is your expected code?
9th Jun 2021, 2:48 PM
Atul [Inactive]
+ 1
visph why you are making it complicated if I==1 || j==1 || j==n || I==n Print* Else space
9th Jun 2021, 6:19 PM
Atul [Inactive]
9th Jun 2021, 2:39 PM
Best of Sport
Best of Sport - avatar
0
Pariket you copied the same answer as that of mine 😅
9th Jun 2021, 4:42 PM
Atul [Inactive]
0
Best of Sport your code print single character but you need to repeat it for some cycles so you have use a loop. You have some solutions already by other posted. I just want to add a change in @visph code that use i<n-1 condition in 1st and last for loop... as for (int i=0; i<n-1; ++i) System.out.print("*");
9th Jun 2021, 5:56 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 if you want to replace n by n-1 in first and last loop conditions, you should also replace < by <= ;P
9th Jun 2021, 6:03 PM
visph
visph - avatar
0
No visph . I mean to shape it by cut edge in 1st and last line to shape D. A more suitable than a rectangle ◻. See output difference.
9th Jun 2021, 6:11 PM
Jayakrishna 🇮🇳
0
Oh. I was thinking about shape in description. (The D pattern).Not about squire. so I was wrong .. visph. So no need any change
9th Jun 2021, 6:18 PM
Jayakrishna 🇮🇳
0
well, I was focused on the question title ^^ to get the exact shape given by OP in description, assuming n would be the number of lines: https://code.sololearn.com/ccNJpso4VWV6/?ref=app
9th Jun 2021, 6:27 PM
visph
visph - avatar