Javatpoint Logo
Javatpoint Logo

Nonagonal Number in Java

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

Nonagonal Number

Nonagonal numbers are the figurate numbers of the form n(7n-5)/2. If n is a nonagonal number, then 7n+3 will be a triangular number. It extends the concept of triangular and square numbers to the nonagon. It is also known as a 9-gonal or enneagonal number. It is an OEIS sequence A001106.

It counts the number of dots in a pattern of n nested nonagons that has 9-sides. All share a common corner, where the i-th nonagon in the pattern has sides made of i dots spaced one unit apart from each other. N2

Nonagonal Number in Java

Types of Nonagonal Numbers

  1. Centered Nonagonal Number
  2. Second 9-gonal or Nonagonal Number
  3. 3-time Nonagonal Number
  4. Twice Nonagonal Number
  5. Palindromic Nonagonal Number

Let's discuss each in detail.

Nonagonal Number Example

First few nonagonal numbers are:

1, 9, 24, 46, 75, 111, 154, 204, 261, 325, 396, 474, 559, 651, 750, 856, 969, 1089, 1216, 1350, 1491, 1639, 1794, 1956, 2125, 2301, 2484, 2674, 2871, 3075, 3286, 3504, 3729, 3961, 4200, 4446, 4699, 4959, 5226, 5500, 5781, 6069, 6364.

Formula to Find Nonagonal Number

We can find the n-th nonagonal number by using the following formula:

Nn = n (7n - 5) / 2

Let's implement the above steps in a Java program.

Nonagonal Number Java Program

NonagonalNumberExample.java

Output:

Enter the term you want to find: 4
The 4 rd/th nonagonal number is: 46

Centered Nonagonal Numbers

It is also a figurate number that can be calculated by using the following formula:

Nn = 9n(n-1)/2+1

It is an OEIS sequence A060544. Note that every third triangular number of the sequence A000217 is a nonagonal number.

Centered Nonagonal Number Example

First few centered nonagonal numbers are:

1, 10, 28, 55, 91, 136, 190, 253, 325, 406, 496, 595, 703, 820, 946, 1081, 1225, 1378, 1540, 1711, 1891, 2080, 2278, 2485, 2701, 2926, 3160, 3403, 3655, 3916, 4186, 4465, 4753, 5050, 5356, 5671, 5995, 6328, 6670, 7021, 7381, 7750, 8128, 8515, 8911, 9316.

Nonagonal Number in Java

Formula to Find Centered Nonagonal Number

We can calculate the centered nonagonal numbers by using the following formula:

Nn = 9n(n-1)/2+1

Centered Nonagonal Number Java Program

CenteredNonagonalNumberExample.java

Output:

Enter the term you want to find: 25
The 25 rd/th centered nonagonal number is: 2701

Second 9-gonal or Nonagonal Number

It is also a figurate number that can be find by using the following formula:

Nn = n*(7*n+5)/2

It is an OEIS sequence A179986.

Second 9-gonal Number Example

First few 9-gonal numbers are:

6, 19, 39, 66, 100, 141, 189, 244, 306, 375, 451, 534, 624, 721, 825, 936, 1054, 1179, 1311, 1450, 1596, 1749, 1909, 2076, 2250, 2431, 2619, 2814, 3016, 3225, 3441, 3664, 3894, 4131, 4375, 4626, 4884, 5149, 5421, 5700, 5986, 6279, 6579, 6886.

Note that the sequence is the bisection of the OEIS sequence A118277 (only even part).

Second 9-gonal Number Java Program

SecondNonagonalNumberExample.java

Output:

Enter the term you want to find: 35
The 35 rd/th second 9-gonal or nonagonal number is: 4375

3-Times Nonagonal Numbers

There is another OEIS sequence A152759 that is also a 9-gonal number or nonagonal number. But it is 3-times of the sequence A001106. It means that we can calculate the 3- times 9-gonal by using any of the following formula:

Nn = 3*(n*(7*n-5)/2)

Or

Nn = (21n^2 - 15n)/2

Or

A001106(n)*3

3-Times Nonagonal Numbers Example

First few 3-time 9-gonal numbers are:

3, 27, 72, 138, 225, 333, 462, 612, 783, 975, 1188, 1422, 1677, 1953, 2250, 2568, 2907, 3267, 3648, 4050, 4473, 4917, 5382, 5868, 6375, 6903, 7452, 8022, 8613, 9225, 9858, 10512, 11187, 11883, 12600, 13338, 14097, 14877, 15678.

3-Times 9-gonal Number Java Program

ThriceNonagonalNumberExample.java

Output:

Enter the term you want to find: 10
The 10 rd/th 3-times nonagonal number is: 975

Twice Nonagonal Number

It is an OEIS sequence A139268. We can calculate the twice nonagonal numbers by using the following formula:

Nn = n(7n-5)

Twice Nonagonal Number Example

First few twice nonagonal numbers are:

2, 18, 48, 92, 150, 222, 308, 408, 522, 650, 792, 948, 1118, 1302, 1500, 1712, 1938, 2178, 2432, 2700, 2982, 3278, 3588, 3912, 4250, 4602, 4968, 5348, 5742, 6150, 6572, 7008, 7458, 7922, 8400, 8892, 9398, 9918, 10452, 11000.

Twice Nonagonal Number Java Program

TwiceNonagonalNumberExample.java

Output:

Enter the term you want to find: 26
The 26 rd/th twice nonagonal number is: 4602

Palindromic Nonagonal Numbers

Palindromic nonagonal numbers are numbers that can be written from both sides (right to left and left to right) without changing the value. A number that has an even number of digits cannot be a palindromic nonagonal number. It is an OEIS sequence A082723.

Palindromic Nonagonal Number Example

First few palindromic nonagonal numbers are:

1, 9, 111, 474, 969, 6666, 18981, 67276, 4411144, 6964696, 15444451, 57966975, 448707844, 460595064, 579696975, 931929139, 994040499, 1227667221, 9698998969, 61556965516, 664248842466, 699030030996, 99451743334715499.

Palindromic Nonagonal Number Java program

The following Java program checks whether the given number is palindromic nonagonal or not.

PalindromicNonagonalExample.java

Output 1:

Enter the number you want to check: 6964696
6964696 is palindromic nonagonal.

Output 2:

Enter the number you want to check: 4411144
4411144 is palindromic nonagonal.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA