A nonagonal number is a figurate number that extends the concept of triangular and square numbers to the nonagon. Specifically, the nth nonagonal numbers count the number of dots in a pattern of n nested nonagons (9 sided polygons), all sharing a common corner, where the ith nonagon in the pattern has sides made of i dots spaced one unit apart from each other.
Examples :
Input : n = 10
n (7n - 5) / 2
10 * (7 * 10 - 5) / 2
10 * 65 / 2 = 325
Output :325
Input : 15
Output :750
The nth nonagonal number is given by the formula: n (7n - 5) / 2.
C++
// CPP Program to find
// nth nonagonal number.
#include <bits/stdc++.h>
using namespace std;
// Function to find nth
// nonagonal number.
int Nonagonal(int n)
{
// Formula to find nth
// nonagonal number.
return n * (7 * n - 5) / 2;
}
// Driver function.
int main()
{
int n = 10;
cout << Nonagonal(n);
return 0;
}
Java
// Java Program to find
// nth nonagonal number.
import java.io.*;
class GFG {
// Function to find nth
// nonagonal number.
static int Nonagonal(int n)
{
// Formula to find nth
// nonagonal number.
return n * (7 * n - 5) / 2;
}
// Driver function.
public static void main(String args[])
{
int n = 10;
System.out.println(Nonagonal(n));
}
}
// This code is contributed by Nikita Tiwari.
Python3
# Python 3 Program to find
# nth nonagonal number.
# Function to find nth
# nonagonal number.
def Nonagonal(n):
# Formula to find nth
# nonagonal number.
return int(n * (7 * n - 5) / 2)
n = 10
print(Nonagonal(n))
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# Program to find
// nth nonagonal number.
using System;
class GFG {
// Function to find nth
// nonagonal number.
static int Nonagonal(int n)
{
// Formula to find nth
// nonagonal number.
return n * (7 * n - 5) / 2;
}
// Driver function.
public static void Main()
{
int n = 10;
Console.Write(Nonagonal(n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP Program to find
// nth nonagonal number.
// Function to find nth
// nonagonal number.
function Nonagonal($n)
{
// Formula to find nth
// nonagonal number.
return $n * (7 * $n - 5) / 2;
}
// Driver Code
$n = 10;
echo Nonagonal($n);
// This code is contributed by vt_m.
?>
JavaScript
<script>
// Javascript Program to find
// nth nonagonal number.
// Function to find nth
// nonagonal number.
function Nonagonal(n)
{
// Formula to find nth
// nonagonal number.
return parseInt(n * (7 * n - 5) / 2);
}
// Driver function.
let n = 10;
document.write(Nonagonal(n));
// This code is contributed by souravmahato348.
</script>
Output :
325
Time Complexity: O(1)
Auxiliary Space: O(1)
Given a number n, find nonagonal number series till n terms.
C++
// CPP Program find first
// n nonagonal number.
#include <bits/stdc++.h>
using namespace std;
// Function to find nonagonal
// number series.
int Nonagonal(int n)
{
for (int i = 1; i <= n; i++)
{
cout << i * (7 * i - 5) / 2;
cout << " ";
}
}
// Driver Code
int main()
{
int n = 10;
Nonagonal(n);
return 0;
}
Java
// Java Program find first
// n nonagonal number.
import java.io.*;
class GFG
{
// Function to find nonagonal
// number series.
static void Nonagonal(int n)
{
for (int i = 1; i <= n; i++)
{
System.out.print(i * (7 *
i - 5) / 2);
System.out.print(" ");
}
}
// Driver Code
public static void main(String args[])
{
int n = 10;
Nonagonal(n);
}
}
// This code is contributed
// by Nikita Tiwari.
Python3
# Python 3 Program find first
# n nonagonal number.
# Function to find nonagonal
# number series.
def Nonagonal(n):
for i in range(1, n+1):
print(int(i * (7 * i - 5) / 2),end=" ")
# driver Function
n = 10
Nonagonal(n)
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# Program find first
// n nonagonal number.
using System;
class GFG
{
// Function to find nonagonal
// number series.
static void Nonagonal(int n)
{
for (int i = 1; i <= n; i++)
{
Console.Write(i * (7 *
i - 5) / 2);
Console.Write(" ");
}
}
// Driver Code
public static void Main()
{
int n = 10;
Nonagonal(n);
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP Program find first
// n nonagonal number.
// Function to find nonagonal
// number series.
function Nonagonal($n)
{
for ( $i = 1; $i <= $n; $i++)
{
echo $i * (7 * $i - 5) / 2;
echo " ";
}
}
// Driver Code
$n = 10;
Nonagonal($n);
// This code is contributed by ajit
?>
JavaScript
<script>
// Javascript Program find first
// n nonagonal number.
// Function to find nonagonal
// number series.
function Nonagonal(n)
{
for (let i = 1; i <= n; i++)
{
document.write(parseInt(i * (7 * i - 5) / 2) + " ");
}
}
// Driver Code
let n = 10;
Nonagonal(n);
</script>
Output :
1 9 24 46 75 111 154 204 261 325
Time Complexity: O(n)
Auxiliary Space: O(1)
Reference:https://en.wikipedia.org/wiki/Nonagonal_number
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem