Practice Pattern 28

package PracticePattern;

import java.util.Scanner;

public class PracticePattern30 {

public static void main(String[] args)

{
	System.out.println("PracticePattern30");
	
	Scanner scan = new Scanner(System.in);
	int n = scan.nextInt();
	
	int rows = 1 ; 
	int nsp = n-1;
	int nst = 1;
	int var = 1;
	while(rows<=n )
	{
		// for space 
		for(int csp = 1;csp<=nsp;csp++)
		{
			System.out.print(" ");
		}
		
		//for work 
		for(int cst = 1;cst<=nst;cst++)
		{
			if(cst == 1 || cst<=2*rows-1  )
			{
			System.out.print(var);
			}
			else if(cst<=nst/2)
		
			{
				System.out.print(var++);
			}
		
			else
			{
				
			System.out.print(var--);
			}
			
		}
		//prep
		System.out.println();
		nsp = nsp-1;
		nst = nst +2;
		var++;
		rows++;
		
	
	}

}

}

Hey @Nitya_Somani
just a change in for loop and var set at rows value
correct code
import java.util.Scanner;

public class Main {

public static void main(String[] args)

{
	System.out.println("PracticePattern30");

	Scanner scan = new Scanner(System.in);
	int n = scan.nextInt();

	int rows = 1;
	int nsp = n - 1;
	int nst = 1;

	while (rows <= n) {
		// for space
		for (int csp = 1; csp <= nsp; csp++) {
			System.out.print(" ");
		}

		// for work
		int var = rows;
		for (int cst = 1; cst <= nst; cst++) {

// if (cst == 1 || cst <= 2 * rows - 1) {
// System.out.print(var);
// } else
if (cst <= nst / 2)

			{
				System.out.print(var++);
			}

			else {

				System.out.print(var--);
			}

		}
		// prep
		System.out.println();
		nsp = nsp - 1;
		nst = nst + 2;
		var++;
		rows++;

	}

}

}

1 Like