Program for centered nonagonal number
Last Updated :
20 Feb, 2023
Centered nonagonal number is a centered figurate number that represents a nonagon with a dot in the center and all other dots surrounding to the center dot in successive nonagonal layers.
The centered nonagonal number for n is given by
Centered nonagonal number = (3 * n - 2) * (3 * n - 1) / 2;
https://en.wikipedia.org/wiki/Centered_nonagonal_number
Examples :
Input : n = 10
Output : 406
Input : n = 8
Output : 253
C++
// CPP Program to find
// nth centered nonagonal number.
#include <bits/stdc++.h>
using namespace std;
// Function to find nth
// centered nonagonal number.
int centeredNonagonal(int n)
{
// Formula to find nth centered
// nonagonal number.
return (3 * n - 2) * (3 * n - 1) / 2;
}
// Driver function.
int main()
{
int n = 10;
cout << centeredNonagonal(n);
return 0;
}
Java
// Java Program to find
// nth centered nonagonal number.
import java.io.*;
class GFG {
// Function to find nth
// centered nonagonal number.
static int centeredNonagonal(int n)
{
// Formula to find nth centered
// nonagonal number.
return (3 * n - 2) * (3 * n - 1) / 2;
}
// Driver function.
public static void main(String args[])
{
int n = 10;
System.out.println(centeredNonagonal(n));
}
}
// This code is contributed by Nikita Tiwari.
Python3
# Python 3 Program to find
# nth centered nonagonal number.
# Function to find nth
# centered nonagonal number
def centeredNonagonal(n) :
# Formula to find nth centered
# nonagonal number.
return (3 * n - 2) * (3 * n - 1) // 2
# Driver function.
n = 10
print(centeredNonagonal(n))
# This code is contributed
# by Nikita Tiwari.
C#
// C# Program to find nth
// centered nonagonal number.
using System;
class GFG {
// Function to find nth
// centered nonagonal number.
static int centeredNonagonal(int n)
{
// Formula to find nth centered
// nonagonal number.
return (3 * n - 2) * (3 * n - 1) / 2;
}
// Driver function.
public static void Main()
{
int n = 10;
Console.Write(centeredNonagonal(n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP Program to find nth
// centered nonagonal number.
// Function to find nth
// centered nonagonal number.
function centeredNonagonal( $n)
{
// Formula to find nth centered
// nonagonal number.
return (3 * $n - 2) *
(3 * $n - 1) / 2;
}
// Driver Code
$n = 10;
echo centeredNonagonal($n);
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript Program to find
// nth centered nonagonal number.
// Function to find nth
// centered nonagonal number.
function centeredNonagonal(n)
{
// Formula to find nth centered
// nonagonal number.
return parseInt((3 * n - 2) * (3 * n - 1) / 2);
}
// Driver function.
let n = 10;
document.write(centeredNonagonal(n));
// This code is contributed by souravmahato348.
</script>
Output:
406
Time Complexity: O(1)
Auxiliary Space: O(1)
Given a number n, find centered nonagonal number series till n.
We can also find the centered nonagonal number series. centered nonagonal number series contains the points on centered nonagonal.
centered nonagonal number : 1, 10, 28, 55, 91, 136, 190, 253, 325, 406, 496, 595, 703, 820, 946 . . .
C++
// CPP Program find first
// n centered nonagonal number.
#include <bits/stdc++.h>
using namespace std;
// Function to find centered
// nonagonal number series.
int centeredNonagonal(int n)
{
for (int i = 1; i <= n; i++) {
cout << (3 * i - 2) * (3 * i - 1) / 2;
cout << " ";
}
}
// Driver function.
int main()
{
int n = 10;
centeredNonagonal(n);
return 0;
}
Java
// Java Program find first
// n centered nonagonal number.
import java.io.*;
class GFG {
// Function to find centered
// nonagonal number series.
static void centeredNonagonal(int n)
{
for (int i = 1; i <= n; i++)
{
System.out.print((3 * i - 2) *
(3 * i - 1) / 2);
System.out.print(" ");
}
}
// Driver function.
public static void main(String args[])
{
int n = 10;
centeredNonagonal(n);
}
}
// This code is contributed
// by Nikita Tiwari.
Python3
# Python3 Program find first
# n centered nonagonal number
# Function to find centered
# nonagonal number series
def centeredNonagonal(n) :
for i in range(1, n + 1) :
print( (3 * i - 2) * (3 * i - 1) // 2, end=" ")
# Driver function
n = 10
centeredNonagonal(n)
# This code is contributed by Nikita Tiwari
C#
// C# Program find first
// n centered nonagonal number.
using System;
class GFG {
// Function to find centered
// nonagonal number series.
static void centeredNonagonal(int n)
{
for (int i = 1; i <= n; i++)
{
Console.Write((3 * i - 2) *
(3 * i - 1) / 2);
Console.Write(" ");
}
}
// Driver function.
public static void Main()
{
int n = 10;
centeredNonagonal(n);
}
}
// This code is contributed by
// by vt_m.
PHP
<?php
// PHP Program find first
// n centered nonagonal number.
// Function to find centered
// nonagonal number series.
function centeredNonagonal( $n)
{
for ( $i = 1; $i <= $n; $i++)
{
echo (3 * $i - 2) *
(3 * $i - 1) / 2;
echo " ";
}
}
// Driver Code
$n = 10;
centeredNonagonal($n);
// This code is contributed anuj_67.
?>
JavaScript
<script>
// JavaScript Program find first
// n centered nonagonal number.
// Function to find centered
// nonagonal number series.
function centeredNonagonal(n)
{
for (let i = 1; i <= n; i++)
{
document.write((3 * i - 2) *
(3 * i - 1) / 2);
document.write(" ");
}
}
// Driver code
let n = 10;
centeredNonagonal(n);
// This code is contributed by sanjoy_62.
</script>
Output :
1 10 28 55 91 136 190 253 325 406
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Program for Centered Icosahedral Number We are given a number n, we need to find n-th centered Icosahedral number.Description: A centered icosahedral number is a centered figurate number that represents an icosahedron.The first few centered icosahedral number series are : 1, 13, 55, 147, 309, 561, 923, 1415, 2057, 2869, 3871, 5083, 6525,
4 min read
Centered nonadecagonal number Given a number n, find the nth Centered Nonadecagonal number. A Centered Nonadecagonal Number represents a dot in the center and other dots surrounding it in successive nonadecagon(19 sided polygon) layers. The first few Centered Nonadecagonal numbers are: 1, 20, 58, 115, 191, 286, 400, 533, 685, 85
4 min read
Program to check if N is a Centered nonadecagonal number Given an integer N, the task is to check if it is a Centered nonadecagonal number or not. Centered nonadecagonal number represents a dot in the centre and other dots surrounding it in successive nonadecagon(19 sided polygon) layers.The first few Centered nonadecagonal numbers are 1, 20, 58, 115, 191
4 min read
Centered Dodecagonal Number Given a number n, find the nth Centered Dodecagonal Number. The Centered Dodecagonal Number represents a dot in the center and other dots surrounding it in successive dodecagonal(12 sided polygon) layers. Examples : Input : 3 Output : 37 Input : 7 Output :253 The first few centered dodecagonal numbe
3 min read
Program to check if N is a Nonagonal Number Given a number N, the task is to check if N is a Nonagonal Number or not. If the number N is an Nonagonal Number then print "Yes" else print "No". Nonagonal Number is a figurate number that extends the concept of triangular and square numbers to the Nonagon. Specifically, the nth Nonagonal Numbers c
4 min read