×

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

List all files in a Directory in Java

The list of all files in the directory can be done using Java. You should be aware that a directory may include a subfolder and that subdirectory may also contain some...

3 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Java Math.multiplyExact() method

In java, we are provided with multiplyExact method in the Math module. This Math module belongs to the java.lang package. In general, this method returns the product of the provided...

2 minutes read.

How to set timer in Java

In this article, you will be very well equipped with the knowledge to set timer in java. The timer in java can be set by using timer class provided by...

3 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.

Thread Scheduler in java

Scheduling: it is defined as the execution of multiple threads on a single CPU in some order is called scheduling. Preemptive-priority scheduling: This algorithm schedules threads based on their priority relative to other...

11 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

Difference between String and Char Array in Java

We are heading to examine some significant differences between String and Character arrays. Both char arrays and String hold the series of characters and are utilised as a cluster of...

3 minutes read.

Java Architecture

Java architecture is a combination of three parts they are JVM, JRE and JDK. These components will help in the functioning of the java programs. The process of code interpretation...

6 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

Java List Interface

List interface is used when we have to order a collection which contains duplicate entries. Like an array, the elements of its implementation classes are retrieved and inserted at a...

2 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Alien language problem in Java

Given the alphabetic sequence of an alien language, given a sorted dictionary (array of words) for the languages. Example: Words = { "aac", "abc", "aaa" } Output c, a, b Algorithm: (1) Compare two words that...

3 minutes read.

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.