卐 ganesha's pattern

able to solve it but not working for N>=5 other than 7 need an approach to solve this

package challenges;

import java.util.Scanner;

public class Ganeshpattern {

public static void main(String[] args) {
	{
		Scanner scan =new Scanner(System.in);
		int N=scan.nextInt();	
		int row=1;
		int nst=1;
		int nsp=(N+1)/4;
		int nst1=(N+1)/2;
		int nsp1=(N+1)/4;
		while(row<=N)
		{
			if(row>=1&&row<=(N+1)/2)
			{
			for(int cst=1;cst<=nst;cst++)
			{
				System.out.print("*");
			}
			for(int csp=1;csp<=nsp;csp++)
			{
				System.out.print(" ");
			}
			
		
			for(int cst1=1;cst1<=nst1;cst1++)
			{
				System.out.print("*");
			}
			}
			if(row==(N+1)/2+1||row==(N+1)/2+2)
			{
				for(int csp=1;csp<=nsp;csp++)
				{
					System.out.print(" ");
				}
				for(int cst=1;cst<=nst;cst++)
				{
					System.out.print("*");
				}
				for(int csp1=1;csp1<=nsp1;csp1++)
				{
					System.out.print(" ");
				}
				for(int cst=1;cst<=nst;cst++)
				{
					System.out.print("*");
				}
				
			}
			
			if(row==N)
			{
				for(int cst1=1;cst1<=nst1;cst1++)
				{
					System.out.print("*");
				}
				for(int csp1=1;csp1<=nsp1;csp1++)
				{
					System.out.print(" ");
				}
				for(int cst=1;cst<=nst;cst++)
				{
					System.out.print("*");
				}
			}
			
		
		System.out.println();
		
		if(row==1)
		{
			nst1=nst1-3;
		}
		if(row==N/2)
		{
			nsp=nsp-2;
			//nsp=0
			//nst1=4
			//nst=3
			nst1=nst1+3;
			nst=nst+2;
		}
		
		if(row==(N+1)/2)
		{
			nsp=nsp+3;
			//nsp=3
			//nst=1
			//nsp1=2
			nst=nst-2;
		}
		
		
		row++;
		
		
		}
		
	}
}

}

Hi @Pradeep,
Actually your code is not working because you have not generalised it . The area of while loop where you change the value of nst and nsp etc you have used nsp=nsp-3 and such that while it should be more general as nsp=nsp-(N+1)/4 . This is just a example not actual correction so do not change code as i have done . Just take this idea and find a general change which include N then your code will run for other value of input other than 7.

tried to resolve it but not able to generalise it there re lots of different probabilities to generalise it and cod enow is not working from 11 onwards please help

package challenges; import java.util.Scanner; public class Ganeshpattern { public static void main(String[] args) { { Scanner scan =new Scanner(System.in); int N=scan.nextInt(); int row=1; int nst=1; int nsp=(N+1)/4; int nst1=(N+1)/2; int nsp1=(N+1)/4; while(row<=N) { if(row>=1&&row<=N-N/2) { for(int cst=1;cst<=nst;cst++) { System.out.print(""); } for(int csp=1;csp<=nsp;csp++) { System.out.print(" “); } for(int cst1=1;cst1<=nst1;cst1++) { System.out.print(”"); } } if(row>=(N+1)/2+(N)/(N-1)&&row<=(N+1)/2+N/3) { for(int csp=1;csp<=nsp;csp++) { System.out.print(" “); } for(int cst=1;cst<=nst;cst++) { System.out.print(”"); } for(int csp1=1;csp1<=nsp1;csp1++) { System.out.print(" “); } for(int cst=1;cst<=nst;cst++) { System.out.print(”"); } } if(row==N) { for(int cst1=1;cst1<=nst1;cst1++) { System.out.print(""); } for(int csp1=1;csp1<=nsp1;csp1++) { System.out.print(" “); } for(int cst=1;cst<=nst;cst++) { System.out.print(”"); } } System.out.println(); if(row==1) { nst1=(N+1)/2-N/2; //nst1=1 } if(row==N/2) { nsp=(N+1)/4-(N+1)/4; //nsp=0 //nst1=4 //nst=3 nst1=(N+1)/2; nst=nst+(N+1)/4; } if(row==(N+1)/2) { nsp=N/2; //nsp=3 //nst=1 //nsp1=2 nst=N/2-(N-2)/2; } row++; } } } }

copy the code in online ide and save it and then share the url please . code makes no sense in my laptop

public static void main(String[] args) {
{
Scanner scan =new Scanner(System.in);
int N=scan.nextInt();
int row=1;
int nst=1;
int nsp=(N+1)/4;
int nst1=(N+1)/2;
int nsp1=(N+1)/4;
while(row<=N)
{
if(row>=1&&row<=N-N/2)
{
for(int cst=1;cst<=nst;cst++)
{
System.out.print("*");
}
for(int csp=1;csp<=nsp;csp++)
{
System.out.print(" ");
}

			for(int cst1=1;cst1<=nst1;cst1++)
			{
				System.out.print("*");
			}
			}
			if(row>=(N+1)/2+(N)/(N-1)&&row<=(N+1)/2+N/3)
			{
				for(int csp=1;csp<=nsp;csp++)
				{
					System.out.print(" ");
				}
				for(int cst=1;cst<=nst;cst++)
				{
					System.out.print("*");
				}
				for(int csp1=1;csp1<=nsp1;csp1++)
				{
					System.out.print(" ");
				}
				for(int cst=1;cst<=nst;cst++)
				{
					System.out.print("*");
				}
				
			}
			
			if(row==N)
			{
				for(int cst1=1;cst1<=nst1;cst1++)
				{
					System.out.print("*");
				}
				for(int csp1=1;csp1<=nsp1;csp1++)
				{
					System.out.print(" ");
				}
				for(int cst=1;cst<=nst;cst++)
				{
					System.out.print("*");
				}
			}
			
		
		System.out.println();
		
		if(row==1)
		{
			nst1=(N+1)/2-N/2;
			//nst1=1
		}
		if(row==N/2)
		{
			nsp=(N+1)/4-(N+1)/4;
			//nsp=0
			//nst1=4
			//nst=3
			nst1=(N+1)/2;
			nst=nst+(N+1)/4;
		}
		
		if(row==(N+1)/2)
		{
			nsp=N/2;
			//nsp=3
			//nst=1
			//nsp1=2
			nst=N/2-(N-2)/2;
		}
		
		
		row++;
		
		
		}
		
	}
}

}

Try to find a more general pattern where you classify using n not values regarding a testcase like 7.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Hey! Here’s my approach to solve this pattern. suggestions are welcome: