Home » Pyramidal Number in Java

Pyramidal Number in Java

by Online Tutorials Library

Pyramidal Number in Java

In this section, we will learn what is a pyramidial number and also create Java programs to check if the given number is a pyramidial number or not. The pyramidial number program is frequently asked in Java coding interviews and academics.

There are two types of Pyramidal numbers

  1. Square Pyramidal Number
  2. Pyramid Number Pattern

Square Pyramidal Number

The sum of the squares of the first natural numbers is called a square pyramidal number. A list of first few square pyramidal number is 1, 5, 14, 30, 55, 91, 140, 204, 285, 385, 506, …

If we try to define the term square pyramidal number in a geometric form these numbers represent number of spheres placed in a stack to form a pyramid with square of each number as a base.

Given a number b (1 <= b <= 1000000000). If b is sum of the squares of the first n natural numbers then print n, otherwise print -1.

Pyramid numbers can be modelled in physical space with a given number of balls and a square frame that hold in place the number of balls forming the base, that is, n2. They also solve the problem of counting the number of squares in an n × n grid.

Pyramidal Number in Java

Square Number Example

Input from user: 55

Output after checking: 5

Explanation: 1*1 + 2*2 + 3*3 + 4*4 + 5*5 = 55

Example:

Input from user: 26

Output after checking: -1

Pyramidial Number Java Program

A Java program to identify whether the number accepted from user is a pyramidal number or not.

PyramidalNumberExample.java

Output:

Enter a number: 14  Position of Pyramidal number 14 is: 3  

In the above Java program, an input s is accepted from the user with the help of the Scanner class method. A method findPyramid() is defined to check whether a number is pyramidal number or not. And the result is displayed on the console using the main() method.

A Java program to identify the position where the pyramidal number stands in the series of pyramidal numbers staring form 1.

SquarePyramidalNumberExample.java

Output:

Square Pyramidal Number at position 5 = 55.0  

In the above Java code, the pyramidal number at the specified position n is calculated using the formula and it stores the value in double variable s. The output is displayed on the console using print() method.

With the help of pyramidial number we can also print pyramid number pattern.

Pyramid Number Pattern

Pyramid number pattern is one of the most commonly asked interview question. It is a way to arrange the numbers in shape of a pyramid.

The numbers can be arranged in shape of a pyramid using different patterns. Some of them are discussed here.

Pyramidal Number in Java

Pyramid Number Pattern Java Program

Java Program for Pattern 1

PyramidPattern1.java

Output:

Enter number of rows for the Pyramid:  9  Pyramid Pattern:            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  

In the above code, the pyramid pattern is built using a nested for loops. Number of rows for the pyramid is accepted from the user. The rowCount is initialized to 1.

Java Program for Pattern 2

PyramidPattern2.java

Output:

Enter number of rows for the Pyramid:  9  Pyramid Pattern:            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  

In the above code, the pyramid pattern is built using a nested for loops. Number of rows for the pyramid is accepted from the user. The rowCount is initialized to 1.

Java Program for Pattern 3

PyramidPattern3.java

Output:

Enter number of rows for the Pyramid:  9  Pyramid Pattern:            *           * *          * * *         * * * *        * * * * *       * * * * * *      * * * * * * *     * * * * * * * *    * * * * * * * * *  

In the above code, the pyramid pattern is built using a nested for loops. Number of rows for the pyramid is accepted from the user. The rowCount is initialized to 1.

Java Program for Pattern 4

PyramidPattern4.java

Output:

Enter number of rows for the pyramid:  9  Pyramid Pattern:                     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  

In the above code, the pyramid pattern is built using a nested for loops. Number of rows for the pyramid is accepted from the user. The rowCount is initialized to 1.

Java Program for Pattern 5

PyramidPattern5.java

Output:

Enter number of rows for the pyramid:  9  Pyramid Pattern:   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  

In the above code, the pyramid pattern is built using a nested for loops. Number of rows for the pyramid is accepted from the user. The rowCount is initialized to nrows.

Java Program for Pattern 6

PyramidPattern6.java

Output:

Enter number of rows for the pyramid:  9  Pyramid Pattern:                     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  

In the above code, the pyramid pattern is built using a nested for loops. Number of rows for the pyramid is accepted from the user. The rowCount is initialized to 1.

In this article, we have discussed what square pyramidal number in Java is, an example to understand the logic behind it, a Java program to identify a pyramidal number, Java programs for different pyramid patterns of numbers.


You may also like