Nonagonal number

A nonagonal number is a figurate number that extends the concept of triangular and square numbers to the nonagon. Specifically, the nth nonagonal numbers counts 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++



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 bt
# Smitha Dinesh Semwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right



Output :

325

Given a number n, find nonagonal number series till n terms .

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 contibuted by ajit
?>

chevron_right



Output :

1 9 24 46 75 111 154 204 261 325 

Reference:https://en.wikipedia.org/wiki/Nonagonal_number



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m, jit_t

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.