Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Natural Numbers
  • Whole Numbers
  • Real Numbers
  • Integers
  • Rational Numbers
  • Irrational Numbers
  • Complex Numbers
  • Prime Numbers
  • Odd Numbers
  • Even Numbers
  • Properties of Numbers
  • Number System
Open In App
Next Article:
Centered Octagonal Number
Next article icon

Octagonal number

Last Updated : 20 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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++




// C++ program to find
// nth octagonal number
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate
//octagonal number
int octagonal(int n)
{
    // Formula for finding 
    // nth octagonal number
    return 3 * n * n - 2 * n;
}
  
// Driver function
int main()
{
    int n = 10;
    cout << n << "th octagonal number :" 
         << octagonal(n);
    return 0;
}
 
 

Java




// Java program to find
// nth octagonal number
import 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




# Python program to find 
# nth octagonal number
def octagonal(n):
    return 3 * n * n - 2 * n
  
# Driver code
n = 10
print(n, "th octagonal number :",
       octagonal(n))
 
 

C#




// C# program to find nth octagonal number
using 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
// PHP program to find
// nth octagonal number
  
  
// Function to calculate
//octagonal number
function 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 .
?>
 
 

Javascript




<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)
 

Given number n, find the octagonal series till n.

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++




// C++ program to display the 
// octagonal series
#include <bits/stdc++.h>
using namespace std;
  
// Function to display
// octagonal series
void 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 function
int main()
{
    int n = 10;
    octagonalSeries(n);
    return 0;
}
 
 

Java




// Java program to find 
// nth octagonal number
import 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




# Python program to find 
# nth octagonal number
def octagonalSeries(n):
    for i in range(1, n + 1):
        print(3 * i * i - 2 * i,
                   end = ", ")
  
# Driver code
n = 10
octagonalSeries(n)
 
 

C#




// C# program to find 
// nth octagonal number
using 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
// PHP program to display the 
// octagonal series
  
  
// Function to display
// octagonal series
function 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 .
?>
 
 

Javascript




<script>
// Javascript program to display the
// octagonal series
  
  
// Function to display
// octagonal series
function 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)
 



Next Article
Centered Octagonal Number

D

dharmendra_kumar
Improve
Article Tags :
  • DSA
  • Mathematical
  • Basic Coding Problems
  • Numbers
  • series
Practice Tags :
  • Mathematical
  • Numbers
  • series

Similar Reads

  • Octadecagonal Number
    Given a number N, the task is to find the Nth Octadecagonal number. An Octadecagonal number is a class of figurate numbers. It has 18 – sided polygon called Octadecagon. The Nth Octadecagonal number count’s the eighteen number of dots and all other dots are surrounding with a common sharing corner a
    3 min read
  • Centered Octagonal Number
    Given a number n, find the nth centered octagonal number. A centered octagonal number represents an octagon with a dot in the center and others dots surrounding the center dot in the successive octagonal layer. Examples : Input : 2 Output : 9 Input : 5 Output : 81 Centered Octagonal n-th Number is g
    4 min read
  • Pentadecagonal Number
    Given a number N, the task is to find the Nth Pentadecagonal number. A Pentadecagonal number is a figurate number that extends the concept of triangular and square numbers to the pentadecagon(a 15-sided polygon). The Nth pentadecagonal number counts the number of dots in a pattern of N nested pentad
    3 min read
  • Tetradecagonal number
    Given a number n, the task is to find the nth tetradecagonal number. A tetradecagonal number is a 14-sided polygon called tetrakaidecagon or tetradecagon and belongs to the figurative number. The nth tetradecagonal number is dotted with some dots and create a series of pattern. They have a common sh
    4 min read
  • n'th Pentagonal Number
    Given an integer n, find the nth Pentagonal number. The first three pentagonal numbers are 1, 5, and 12 (Please see the below diagram). The n'th pentagonal number Pn is the number of distinct dots in a pattern of dots consisting of the outlines of regular pentagons with sides up to n dots when the p
    7 min read
  • Centered Octadecagonal Number
    Given a number n, find the nth Centered Octadecagonal number. The Centered Octadecagonal Number represents a dot in the center and others dot are arranged around it in successive layers of octadecagon(18 sided polygon). Examples : Input : 2 Output : 19 Input : 6 Output : 271 In mathematics, Centered
    4 min read
  • Triacontakaihenagonal number
    A triacontakaihenagonal number is a class of figurate numbers. It has 31 - sided polygon called triacontakaihenagon. The N-th triacontakaihenagonal number count’s the 31 number of dots and all other dots are surrounding with a common sharing corner and make a pattern.The first few triacontakaihenago
    3 min read
  • Pentagonal Pyramidal Number
    Given a number n, find the nth pentagonal pyramidal number.A Pentagonal Pyramidal Number belongs to the figurate number class. It is the number of objects in a pyramid with a pentagonal base. The nth pentagonal pyramidal number is equal to sum of first n pentagonal numbers. Examples: Input : n = 3 O
    6 min read
  • Pentatope Numbers
    Pentatope numbers represent how many small spheres you can fit together in a four-dimensional shape known as a Pentatope (4-dimensional tetrahedron). A pentatope is a four-dimensional geometric shape, the simplest form of a four-dimensional simplex, consisting of 5 vertices, 10 edges, and 10 triangu
    4 min read
  • Program to check if N is a Octagonal Number
    Given a number N, the task is to check if N is an Octagonal Number or not. If the number N is an Octagonal Number then print "Yes" else print "No". Octagonal Number is the figure number that represent octagonal. Octagonal Numbers can be formed by placing triangular numbers on the four sides of a squ
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences