Only Institute
to provide recording daily after class to students. you can practice whole day under faculty guidance. where test and mockup tests are taken regularly. provides all mobile nos of old students on website.

How to create pyramid of numbers

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 By Kiran
Java By Kiran In this post, we will try to write the java programs to create pyramid of numbers in all different patterns.

Pattern 1 : Write java program to create pyramid of numbers like in Pattern1 of the above image?

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




import java.util.Scanner;
 
public class MainClass
{
    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++;
        }
    }
}


Output :
How Many Rows You Want In Your Pyramid?
9
Here Is Your Pyramid
         1
        2 2
       3 3 3
      4 4 4 4
     5 5 5 5 5
    6 6 6 6 6 6
   7 7 7 7 7 7 7
  8 8 8 8 8 8 8 8
 9 9 9 9 9 9 9 9 9

Pattern 2 : How to create pyramid of numbers in Java like in Pattern2 of the above image?

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.





import java.util.Scanner;
 
public class MainClass
{
    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++;
        }
    }
}


Output :
How Many Rows You Want In Your Pyramid?
9
Here Is Your Pyramid

         1
        1 2
       1 2 3
      1 2 3 4
     1 2 3 4 5
    1 2 3 4 5 6
   1 2 3 4 5 6 7
  1 2 3 4 5 6 7 8
 1 2 3 4 5 6 7 8 9

Pattern 3 : Write a java program to create pyramid of stars(*) like in the Pattern3 of the above image?

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



public class MainClass
{
    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++;
        }
    }
}


Output :
How Many Rows You Want In Your Pyramid?
9
Here Is Your Pyramid

	 *
        * *
       * * *
      * * * *
     * * * * *
    * * * * * *
   * * * * * * *
  * * * * * * * *
 * * * * * * * * *

Pattern 4 : Write java program to print pyramid of numbers like in the Pattern4 of the above image?

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.



import java.util.Scanner;
 
public class MainClass
{
    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++;
        }
    }
}


Output :
How Many Rows You Want In Your Pyramid?
Output :
How Many Rows You Want In Your Pyramid?
9
Here Is Your Pyramid

                  1
                1 2 1
              1 2 3 2 1
            1 2 3 4 3 2 1
          1 2 3 4 5 4 3 2 1
        1 2 3 4 5 6 5 4 3 2 1
      1 2 3 4 5 6 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1


Pattern 5 : Write Java program to print reverse pyramid of numbers like in the Pattern5 of the above image?

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




import java.util.Scanner;
 
public class MainClass
{
    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--;
        }
    }
}


Output :
How Many Rows You Want In Your Pyramid?
9
Here Is Your Pyramid
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1
  1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
    1 2 3 4 5 6 7 6 5 4 3 2 1
      1 2 3 4 5 6 5 4 3 2 1
        1 2 3 4 5 4 3 2 1
          1 2 3 4 3 2 1
            1 2 3 2 1
              1 2 1
				1

Pattern 6 : How do you create pyramid of numbers like in the Pattern6 of the above image?

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.




        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++;
        }
    }
}


Output :
How Many Rows You Want In Your Pyramid?
9
Here Is Your Pyramid

                9
              8 9 8
            7 8 9 8 7
          6 7 8 9 8 7 6
        5 6 7 8 9 8 7 6 5
      4 5 6 7 8 9 8 7 6 5 4
    3 4 5 6 7 8 9 8 7 6 5 4 3
  2 3 4 5 6 7 8 9 8 7 6 5 4 3 2
1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1