Pattern Problem

In patternQuesPdf question number: 8, I wrote the code and it is working perfectly fine for input 5, but when I try to test the code for input 7 I am getting the wrong output.
Can anyone please solve the issues.

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int row=1, nst=1, nsp1=0, nsp2=n/2+1;
while(row<=n){
for(int csp=1; csp<=nsp1; csp++)
System.out.print(" \t");

    for(int cst=1; cst<=nst; cst++)
        System.out.print("*\t");
    for(int csp=1; csp<=nsp2; csp++)
        System.out.print(" \t");
    for(int cst=1; cst<=nst; cst++){
        if(row!=n/2+1)
            System.out.print("*\t");

    }

    System.out.println();
    if(row<=n/2){
        nsp1++;
        nsp2-=2;
    }else
    {
        nsp1--;
        nsp2+=2;
    }
    row++;
   }
}

}

here


your code was almost correct just used spaces

When printing the space you are just providing one single space(one character) while printing the * you are providing * with one space(two characters), so I need to do always like this or sometimes. Before this, I solved 7 questions with one character only like sop(*) and sop(" ")

this is no rule .just look at the pattern and form your own way each time.you were giving one extra space along with the tab .

if this solves your doubt please mark it as resolved :slight_smile:

Hey, I got the correct answer, nsp2 variable should be initialized by n-2, I was initializing with n/2+1. anyway thanks.