×

Nonagonal number in Java

Figureate numbers with the form n(7n-5)/2 are known as nonagonal numbers. 7n+3 will be a triangular number if n is a nonagonal number. The nonagon is included in the idea of triangular and square numbers. It is often referred to as an enneagonal or 9-gonal number. The sequence is A001106 from the OEIS.

The first few numerical values 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

Program for Nonagonal number using Java

Nonagonal.java
// importing required packages
import java. util. * ; 
import java.io . * ;
// public class is declared with the name Nonagonal 
public class Nonagonal
{ 
// static method is declared to find the Nonagonal number in the required position
static int Nonagonal ( int k ) 
{ 
// returning the value
return k * (7 * k - 5) / 2; 
} 
// Main section where execution of the program starts
public static void main(String args[]) 
{ 
// Creating object for scanner class to take inputs from the user during run time
Scanner sc = new Scanner ( System . in ) ; 
// Enter the position 
System . out . println ( " Enter the term position you want to find: " ) ; 
// Storing the value in the integer variable n
int n = sc . nextInt ( ) ; 
// printing the nonagonal number
System.out.println ( " The "+n+ " nonagonal number is: "+ Nonagonal(n) ) ; 
} 
} 

Output

Nonagonal number in Java

Types of Nonagonal Numbers

Centered Nonagonal Numbers

The following formula may be used to determine it as a figurate number:

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

The sequence is A060544 from the OEIS. In the sequence A000217, it should be noted that each and every third triangular number is a nonagonal number.

Following are the centered nonagonal numbers:

1, 10 , 28 , 55 , 91 , 136 , 190 , 253 , 325 , 406 , 496 , 595 , 703 , 820 , 946 , 1081 , 1225, 1378, 1540, 1711, 1891, 2080, 2278

Program for Centered Nonagonal number using Java

CenteredNon.java

// importing the required packages
import java. util . Scanner ; 
import java . io .* ;
// public class declaration
public class CenteredNon 
{ 
// static method Nonagonal is declared 
static int Nonagonal ( int n ) 
{ 
// return the centered nonagonal number using the formula 9 * n * (n-1) / 2 + 1 ;
return 9 * n * ( n-1 ) / 2 + 1 ; 
} 
// Main section where execution of the program starts 
public static void main ( String args[ ] ) 
{ 
// creating object for scanner class to take inputs from the user during run time
Scanner sc = new Scanner ( System . in ) ; 
System . out . println ( " Enter term position: " ) ; 
int n=sc . nextInt ( ) ; 
System . out . println ( " The " + n + " rd/th centered nonagonal number is : " + Nonagonal ( n ) ) ; 
} 
}

Output

Nonagonal number in Java

Second 9-gonal

The following formula may be used to find it as a figurate number:

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

The sequence is A179986 from the OEIS.

Program for Second 9-gonal number using Java

Second9gonal.java

// importing the required packages
import java . util . Scanner ; 
import java . io . * ;
// public class declaration
public class Second9gonal
{
// static method Nonagonal is declared 
static int Nonagonal ( int n ) 
{ 
// return the second9 gonal number using the formula n * ( 7 * n+5 ) / 2 ;
return n * ( 7 * n + 5) / 2 ; 
} 
// Main section where execution of the program starts 
public static void main ( String args [ ] ) 
{ 
// creating object for scanner class to take inputs from the user during run time
Scanner sc = new Scanner ( System . in ) ; 
System . out . println ( "Enter the position : " ); 
// storing the position in an integer variable n
int n = sc . nextInt ( ) ; 
System . out . println ( "The " + n + " centered nonagonal number is: " + Nonagonal ( n ) ) ; 
} 
}

Output

Nonagonal number in Java

3-Times Nonagonal Numbers

The OEIS sequence A152759 is another example of a 9-gonal or nonagonal number. But it repeats the sequence A001106 three times. This indicates that any of the following formulas may be used to compute the 3-by-9-gonal:

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

Program for 3 time Nonagonal number

Threetimesnonagonal.java

// importing the required packages
import java . util . Scanner ; 
import java . io .* ;
// public class declaration
public class Threetimesnonagonal
{
// static method Nonagonal is declared 
static int Nonagonal ( int n ) 
{ 
// return the centered nonagonal number using the formula 3 * ( n * ( 7 * n-5) /2 );
return 3 * ( n * ( 7 * n-5) / 2 ) ; 
} 
// Main section where execution of the program starts 
public static void main( String args[ ] ) 
{ 
// creating object for scanner class to take inputs from the user during run time
Scanner sc = new Scanner( System . in); 
System . out . println ( "Enter position : " ) ; 
// storing the value of the position in an integer variable n
int n=sc . nextInt ( ); 
System . out . println ( " The "+ n + " th 3 times nonagonal number is : "+ Nonagonal(n) ) ; 
} 
}

Output

Nonagonal number in Java

Twice Nonagonal Number

The sequence is A139268 from the OEIS. We may use the following formula to determine the twice-nonagonal numbers:

Nn = n (7n-5)

Program for Twice Nonagonal in Java

TwiceNonagonal.java

// importing the required packages
import java . util . Scanner ; 
import java . io . * ;
// public class declaration
public class TwiceNonagonal
{
// static method Nonagonal is declared 
static int Nonagonal ( int n ) 
{ 
// return the centered nonagonal number using the formula 3 * ( n * ( 7 * n-5 ) / 2 ) ;
return n * ( 7 * n-5 ) ; 
} 
// Main section where execution of the program starts 
public static void main (String args [ ] ) 
{ 
// creating object for scanner class to take inputs from the user during run time
Scanner sc = new Scanner ( System . in ) ; 
System . out . println ( " Enter the position : " ) ; 
// storing it to an integer variable n
int n = sc . nextInt ( ) ; 
System . out . println ( " The " + n + " th 2 times nonagonal number is : "+ Nonagonal ( n ) ) ; 
} 
}

Output

Nonagonal number in Java

Nonagonal Palindromic Numbers

Numbers that may be written both ways (from the right to the left and from the left to the right) without affecting their value are known as palindromic nonagonal numbers. A palindromic nonagonal number cannot have an even number of digits. A08272 is the OEIS sequence.

Program for Nonagonal Palindrome

PalindromeNonagonal.java

import java . util . Scanner ; 
public class PalindromeNonagonal 
{ 
public static void main ( String [ ] args ) 
{ 
int n , rev = 0, rem ; 
Scanner sc = new Scanner ( System.in ) ; 
System . out . print ( " Enter the number you want to check : " ) ; 
n=sc . nextInt () ; 
int a = n ; 
// Logic to reverse a number
while (n != 0 ) 
{ 
rem = n % 10 ; 
rev = rev * 10 + rem ; 
n = n/10 ; 
} 
// a is equal to rev then it will print the given number is a nonagonal palindrome
if (a == rev) 
{ 
System . out . println ( a + " is nonagonal palindrome " ) ; 
} 
// a is not equal to rev then it will print the given number is not a nonagonal palindrome
else 
{ 
System . out . println ( a + " is not nonagonal palindromic " ) ; 
} 
} 
} 

Output

Nonagonal number in Java

Related Topics

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

Java Math rint() Method

The rint() method of Java Math class returns the double value which is close to the specified argument and is equal to mathematical integer. Syntax: public static double rint(double a) Parameters: The parameter ‘a’...

1 minute read.

How to Convert long to String in Java

How to Convert long to String in java It is used if we have to display a long number in the text field because everything is displayed as a String. A...

3 minutes read.

How to check the Java version in cmd

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated...

5 minutes read.

Java Solid Principles

Java implements the object-oriented SOLID principles for the design of software architecture. Solid Principles Java implements the object-oriented SOLID principles for the design of software architecture.   Five guiding principles transformed...

4 minutes read.

Access Modifier in Java

The access modifiers in java are used to change the accessibility and scope of a method, constructor, class, and fields. If you are aware of C++ language, when we declare any member...

2 minutes read.

Java Get Time in UTC

UTC is the abbreviation for Universal Time Coordinated. Before the beginning of UTC, it is mentioned as the Greenwich Mean Time (GMT) but Now it is mentioned as the universal...

4 minutes read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Java Arrays Fill

We may use the Arrays.fill () function to fill a whole array or a subset of it. Arrays.fill () may fill both 2D and 3D arrays. Syntax: Arrays.fill(boolean[] fillArr, int fromIndex, int toIndex, boolean val )   Parameters: The array to be filled...

4 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Java String vs StringBuffer

Java String vs StringBuffer In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes. String...

4 minutes read.

Java Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

Java Integer divideUnsigned() method

The divideUnsigned() method of Integer class returns the unsigned quotient by dividing the first argument by the second argument. Syntax public static int divideUnsigned (int dividend , int divisor) Parameters The parameter ‘dividend’ represents...

1 minute read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Difference between = = and equals ( ) in java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

6 minutes read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

Java File Extension

A computer file's suffix is called its file extension. It is easily recognized since it appears immediately after a period (.) in the file name. Consider the file Demo.java as an...

3 minutes read.