Scanner scn = new Scanner(System.in);
int n =scn.nextInt();
int nsp=4;;
int nst=5;
int row=1;
while(row<=n) {
//for spACE
for (int csp=1; csp<=nsp;csp++) {
System.out.print(" ");
}
//for stars
for(int cst=1;cst<=nst;nst++) {
if(row==1||row==n) {
System.out.print("*");
}else {
for(int i =1; i<n;i++) {
if(row==2||row<n) {
System.out.print();
}
}
}
}
System.out.println();
nsp--;
row++;
}
}
}
Can u please tell me that in my code how to put spaces so that it becomes hollow i am quite confused!
Hey @kalindiyadav5
correct code :
Dry run this code If you donβt understand
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int nsp = n - 1;
;
int nst = n;
int row = 1;
while (row <= n) {
// for spACE
for (int csp = 1; csp <= nsp; csp++) {
System.out.print(" ");
}
// for stars
if (row == 1 || row == n) {
for (int cst = 1; cst <= n; cst++) {
System.out.print("*");
}
} else {
for (int cst = 1; cst <= n; cst++) {
if (cst == 1 || cst == n) {
System.out.print("*");
}
if (row == 2 || row < n) {
System.out.print(" ");
}
}
}
System.out.println();
nsp--;
row++;
}
}
}
1 Like
Thank you so much for the correction.