Solve this pattern
Hii Sumit,
What problem are you facing in this question??
import java.util.Scanner;
public class pattern16
{
public static void main(String args[])
{
Scanner scn=new Scanner(System.in);
int n=scn.nextInt();
int nst=3;
int nsp=1;
int nr=2n-3;
for(int row=1;row<=nr;row++)
{
for(int cst=1;cst<=nst;cst++)
{
System.out.print("");
}
if(row==1||row==7)
System.out.print("");
else
{
for(int csp=1;csp<=nsp;csp++)
{
System.out.print(" “);
}}
for(int cst=1;cst<=nst;cst++)
{
System.out.print(”");
}
System.out.println();
if(row<=nr/2)
{
nst-=1;
nsp+=2;
}
else
{
nst+=1;
nsp-=2;
}}}}
here’s my approach on this pattern . I was seeking for an alternative approach!
Hii Sumit,
There is a mistake in your approach. As in input you will be given the no. of rows. So there is no need of creating a new variable nr. Also your approach will work for only n = 7. You should generalize your approach first.