what changes can get me a pyramid of stars in this code?
chanbhatia (0)

import java.util.Scanner;
class Main {
static int i , j , row;
public static void pyramidPattern(int row) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of Rows");
row = sc.nextInt();
for ( i = 0; i <row; i++) {
System.out.print(" ");
for ( j = 0; j <=i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
public static void main(String args[]){
pyramidPattern(row);
}
}

You are viewing a single comment. View All
ArchieMaclean (584)

Here is a modification of your code that produces a pyramid shape:
https://repl.it/@ArchieMaclean/pyramid

There are comments in the code to show what has changed and why.

Please mark this as the answer if it solved your problem :)

Code (in case the link doesn't work):

import java.util.Scanner;
class Main {
static int i , j , row;
public static void pyramidPattern(int row) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of Rows");
row = sc.nextInt();
for ( i = 0; i <row; i++) {
	for (int s=0;s<(row/2-i/2);s++) System.out.print("  ");	// Add in spaces
	if (i%2 == 0) System.out.print(" ");	// if the row number is even, add an extra space to offset
	for ( j = 0; j <=i; j++) {
		System.out.print("* ");
	}
	System.out.print("\n");
}
}
public static void main(String args[]){
pyramidPattern(row);
}
}
ArchieMaclean (584)

@chanbhatia There should be an option on this page to mark one of the answers as correct? Make sure you're logged in/refresh?