Pattern Practice Question no7

Is this code optimised?

import java.util.;
public class pattern {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
//no of lines
for(int i=1;i<=n;i++){
//no of stars
if(i==1 || i==5){
for(int j=1;j<=n;j++){
System.out.print("
");
}
}
else{
//first 1 star
//then 3 spaces
//then 1 star
System.out.print("");
System.out.print(" “);
System.out.print(” “);
System.out.print(” “);
System.out.print(”
");
}
System.out.println();

    }
    
}

}

The question is the output of this code i just wanted to know that is it optimised or not

https://ide.codingblocks.com/s/72060

Hi @donnaghai

Your code works right when n = 5. But doesn’t work for any other case. You need to write a code which works well for all the values within the constraints.

I guess now its fine
https://ide.codingblocks.com/s/72060

soryy its also wrong.
can u please provide me the code for this

I think i solved it
https://ide.codingblocks.com/s/72060

Yes, the code works fine now.

Hi
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.