How to create pyramid of numbers in Java?

This is one of the frequently asked java interview question for freshers. This question tests the candidate’s logical ability as well as the basic understanding of Java language. They can ask you to create pyramid of numbers with different patterns like in the below image:

Java Number Pyramid Patterns

In this chapter, we will try to write the java programs to create pyramid of numbers in all different patterns one by one.

Java Number Pyramid Pattern 1

  • Take the input from the user and assign it to noOfRows. This will be the number of rows he wants in a pyramid.

  • Define one variable called rowCount and initialize it to 1. This will hold the value of current row count.

  • At the beginning of each row, we print ‘i’ spaces where ‘i’ will be value from noOfRows to 1. At the end of each row, we print rowCount value rowCount times. i.e in the first row, 1 will be printed once. In the second row, 2 will be printed twice and so on.

  • Below is the java code which implements this logic.

  • Write the code for binary search algorithm as follows:

package com.javabykiran.patterns;
import java.util.Scanner;
public class JbkPyramidPattern1 {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);

	// Taking noOfRows value from the user
	System.out.println("How Many Rows You Want In Your Pyramid?");
	int noOfRows = sc.nextInt();

	// Initializing rowCount with 1
	int rowCount = 1;
	System.out.println("Here Is Your Pyramid");

	// Implementing the logic
	for (int i = noOfRows; i > 0; i--) {
            // Printing i spaces at the beginning of each row
            for (int j = 1; j <= i; j++) {
		System.out.print(" ");
            }

            // Printing 'rowCount' value 'rowCount' times at the end of each row
            for (int j = 1; j <= rowCount; j++) {
		System.out.print(rowCount + " ");
            }
            System.out.println();

            // Incrementing the rowCount
            rowCount++;
	}
    }
}
Java Number Pyramid Pattern 2

In this pattern also, we use same logic but instead of printing rowCount value rowCount times at the end of each row, we print ‘j’ where j value will be from 1 to rowCount.

package com.javabykiran.patterns;
import java.util.Scanner;

public class JbkPyramidPattern2 {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);

	// Taking noOfRows value from the user
	System.out.println("How Many Rows You Want In Your Pyramid?");
	int noOfRows = sc.nextInt();

        // Initializing rowCount with 1
	int rowCount = 1;
	System.out.println("Here Is Your Pyramid");

	// Implementing the logic
	for (int i = noOfRows; i > 0; i--) {
            // Printing i spaces at the beginning of each row
            for (int j = 1; j <= i; j++) {
		System.out.print(" ");
            }

            // Printing 'j' value at the end of each row
            for (int j = 1; j <= rowCount; j++) {
		System.out.print(j + " ");
            }
            System.out.println();

	    // Incrementing the rowCount
            rowCount++;
	}
    }
}
Java Number Pyramid Pattern 3

The same logic is used here also. But, instead of printing rowCount or j value at the end of each row, we print star(*).

package com.javabykiran.patterns;
import java.util.Scanner;

public class JbkPyramidPattern3 {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);

	// Taking noOfRows value from the user
	System.out.println("How Many Rows You Want In Your Pyramid?");
	int noOfRows = sc.nextInt();

	// Initializing rowCount with 1
	int rowCount = 1;
	System.out.println("Here Is Your Pyramid");

	// Implementing the logic
	for (int i = noOfRows; i > 0; i--) {
            // Printing i spaces at the beginning of each row
            for (int j = 1; j <= i; j++) {
                System.out.print(" ");
            }

            // Printing * at the end of each row
            for (int j = 1; j <= rowCount; j++) {
                System.out.print("*");
            }
            System.out.println();

            // Incrementing the rowCount
            rowCount++;
	}
    }
}
Java Number Pyramid Pattern 4

In this problem, we print i*2 spaces at the beginning of each row instead of just i spaces. At the end of each row, we print ‘j’ where j value will be from 1 to rowCount and from rowCount-1 to 1.

package com.javabykiran.patterns;
import java.util.Scanner;

public class JbkPyramidPattern4 {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);

	// Taking noOfRows value from the user
	System.out.println("How Many Rows You Want In Your Pyramid?");
	int noOfRows = sc.nextInt();

	// Initializing rowCount with 1
	int rowCount = 1;
	System.out.println("Here Is Your Pyramid");

	// Implementing the logic
	for (int i = noOfRows; i > 0; i--) {		
            // Printing i*2 spaces at the beginning of each row
            for (int j = 1; j <= i * 2; j++) {
                System.out.print(" ");
            }

            // Printing j value where j value will be from 1 to rowCount
            for (int j = 1; j <= rowCount; j++) {
		System.out.print(j + " ");
            }

            // Printing j value where j value will be from rowCount-1 to 1
            for (int j = rowCount - 1; j >= 1; j--) {
		System.out.print(j + " ");
            }
            System.out.println();

            // Incrementing the rowCount
            rowCount++;
        }
    }
}
Java Number Pyramid Pattern 5

In this problem, we iterate outer loop in the reverse order i.e from 1 to noOfRows and initialize the rowCount to noOfRows.

package com.javabykiran.patterns;
import java.util.Scanner;

public class JbkPyramidPattern5 {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);

	// Taking noOfRows value from the user
	System.out.println("How Many Rows You Want In Your Pyramid?");
	int noOfRows = sc.nextInt();

	// Initializing rowCount with noOfRows
	int rowCount = noOfRows;
	System.out.println("Here Is Your Pyramid");

	// Implementing the logic
	for (int i = 0; i < noOfRows; i++) {
			
        // Printing i*2 spaces at the beginning of each row
	for (int j = 1; j <= i * 2; j++) {
            System.out.print(" ");
	}

	// Printing j where j value will be from 1 to rowCount
        for (int j = 1; j <= rowCount; j++) {
            System.out.print(j + " ");
	}

	// Printing j where j value will be from rowCount-1 to 1
	for (int j = rowCount - 1; j >= 1; j--) {
            System.out.print(j + " ");
	}
	System.out.println();

	// Decrementing the rowCount
	rowCount--;
	}
    }
}
Java Number Pyramid Pattern 6

In this problem, at the end of each row we print ‘j’ where ‘j’ value will be from i to noOfRows and from noOfRows-1 to i.

package com.javabykiran.patterns;
import java.util.Scanner;

public class JbkPyramidPattern6 {
    public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);

	// Taking noOfRows value from the user
	System.out.println("How Many Rows You Want In Your Pyramid?");
	int noOfRows = sc.nextInt();

	// Initializing rowCount with 1
	int rowCount = 1;
	System.out.println("Here Is Your Pyramid");

	// Implementing the logic
	for (int i = noOfRows; i >= 1; i--) {
	
            // Printing i*2 spaces at the beginning of each row
            for (int j = 1; j <= i * 2; j++) {
		System.out.print(" ");
            }
                        
            // Printing j where j value will be from i to noOfRows
	    for (int j = i; j <= noOfRows; j++) {
		System.out.print(j + " ");
            }

            // Printing j where j value will be from noOfRows-1 to i
            for (int j = noOfRows - 1; j >= i; j--) {
		System.out.print(j + " ");
            }
            System.out.println();

            // Incrementing the rowCount
            rowCount++;
	}
    }
}