Octagonal number

16 Jul, 2021
You are given a number n, the task is to find nth octagonal number. Also, find the Octagonal series till n.An octagonal number is the figure number that represent octagonal. Octagonal numbers can be formed by placing triangular numbers on the four sides of a square. Octagonal number is calculated by using the formula (3n2 – 2n). Examples :  
Input : 5
Output : 65

Input : 10
Output : 280

Input : 15
Output : 645
 
 
C++
Java
Python
C#
PHP
Javascript
// C++ program to find// nth octagonal number#include <bits/stdc++.h>using namespace std; // Function to calculate//octagonal numberint octagonal(int n){    // Formula for finding    // nth octagonal number    return 3 * n * n - 2 * n;} // Driver functionint main(){    int n = 10;    cout << n << "th octagonal number :"         << octagonal(n);    return 0;}
// Java program to find// nth octagonal numberimport java.util.*;import java.lang.*; public class GfG {     // Function to calculate    //octagonal number    public static int octagonal(int n)    {        // Formula for finding        // nth octagonal number        return 3 * n * n - 2 * n;    }     // Driver function    public static void main(String argc[])    {        int n = 10;        System.out.println(n + "th octagonal" +                     " number :" + octagonal(n));    }} /* This code is contributed by Sagar Shukla */
# Python program to find# nth octagonal numberdef octagonal(n):    return 3 * n * n - 2 * n # Driver coden = 10print(n, "th octagonal number :",       octagonal(n))
// C# program to find nth octagonal numberusing System; public class GfG {     // Function to calculate    //octagonal number    public static int octagonal(int n)    {                 // Formula for finding        // nth octagonal number        return 3 * n * n - 2 * n;    }     // Driver function    public static void Main()    {        int n = 10;                 Console.WriteLine(n + "th octagonal"              + " number :" + octagonal(n));    }} /* This code is contributed by Vt_m */
<?php// PHP program to find// nth octagonal number  // Function to calculate//octagonal numberfunction octagonal($n){         // Formula for finding    // nth octagonal number    return 3 * $n * $n - 2 * $n;}     // Driver Code    $n = 10;    echo $n , "th octagonal number :"                     , octagonal($n);                      // This code is contributed by Vt_m .?>
<script> // JavaScript program to convert// Binary code to Gray code     // Function to calculate    //octagonal number    function octagonal(n)    {        // Formula for finding        // nth octagonal number        return 3 * n * n - 2 * n;    }  // Driver code                          let n = 10;        document.write(n + "th octagonal" +                     " number :" + octagonal(n));       // This code is contributed by code_hunt.</script>
Output :  
10th octagonal number : 280
Time Complexity: O(1)Auxiliary Space: O(1) 
We can also find the octagonal series. Octagonal series contains the points on octagonal.  
Octagonal series 1, 8, 21, 40, 65, 96, 133, 176, 225, 280, . . .
 
C++
Java
Python
C#
PHP
Javascript
// C++ program to display the// octagonal series#include <bits/stdc++.h>using namespace std; // Function to display// octagonal seriesvoid octagonalSeries(int n){    // Formula for finding    //nth octagonal number    for (int i = 1; i <= n; i++)         // Formula for computing        // octagonal number        cout << (3 * i * i - 2 * i);   } // Driver functionint main(){    int n = 10;    octagonalSeries(n);    return 0;}
// Java program to find// nth octagonal numberimport java.util.*;import java.lang.*;  public class GfG {      // Function to display octagonal series    public static void octagonalSeries(int n)    {        // Formula for finding        //nth octagonal number        for (int i = 1; i <= n; i++)              // Formula for computing            // octagonal number            System.out.print(3 * i * i - 2 * i);    }      // Driver function    public static void main(String argc[])    {        int n = 10;        octagonalSeries(n);    }      /* This code is contributed by Sagar Shukla */}
# Python program to find# nth octagonal numberdef octagonalSeries(n):    for i in range(1, n + 1):        print(3 * i * i - 2 * i,                   end = ", ") # Driver coden = 10octagonalSeries(n)
// C# program to find// nth octagonal numberusing System; public class GfG {     // Function to display octagonal series    public static void octagonalSeries(int n)    {                 // Formula for finding        //nth octagonal number        for (int i = 1; i <= n; i++)             // Formula for computing            // octagonal number            Console.Write(3 * i * i - 2 * i + ", ");    }     // Driver function    public static void Main()    {        int n = 10;                 octagonalSeries(n);    }} /* This code is contributed by Vt_m */
<?php// PHP program to display the// octagonal series  // Function to display// octagonal seriesfunction octagonalSeries($n){         // Formula for finding    // nth octagonal number    for ($i = 1; $i <= $n; $i++)         // Formula for computing        // octagonal number        echo (3 * $i * $i - 2 * $i),",";}     // Driver Code    $n = 10;    octagonalSeries($n); // This code is contributed by Vt_m .?>
<script>// Javascript program to display the// octagonal series  // Function to display// octagonal seriesfunction octagonalSeries(n){         // Formula for finding    // nth octagonal number    for (let i = 1; i <= n; i++)         // Formula for computing        // octagonal number        document.write(3 * i * i - 2 * i + ", ");}     // Driver Code    let n = 10;    octagonalSeries(n); // This code is contributed by _saurabh_jaiswal </script>
Output :  
1, 8, 21, 40, 65, 96, 133, 176, 225, 280
Time Complexity: O(n)Auxiliary Space: O(1) 
vt_m
code_hunt
_saurabh_jaiswal
manikarora059
Numbers
series
Mathematical
School Programming
Mathematical
series
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java