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
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Program to check if N is a Octadecagon number
Next article icon

Program to check if N is a Octagonal Number

Last Updated : 22 Dec, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 square. The first few Octagonal Numbers are 1, 8, 21, 40, 65, 96 … 

Examples: 

Input: N = 8 
Output: Yes 
Explanation: 
Second octagonal number is 8.

Input: N = 30 
Output: No

Approach: 

1. The Kth term of the octagonal number is given as
[Tex]K^{th} Term = 3*K^{2} – 2*K     [/Tex]
2. As we have to check whether the given number can be expressed as an Octagonal Number or not. This can be checked as: 

=> [Tex]N = 3*K^{2} – 2*K     [/Tex]
=> [Tex]K = \frac{2 + \sqrt{12*N + 4}}{6}     [/Tex]

3. If the value of K calculated using the above formula is an integer, then N is an Octagonal Number.
4. Else N is not an Octagonal Number.

Below is the implementation of the above approach:

C++

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check if N is a
// Octagonal Number
bool isoctagonal(int N)
{
    float n
        = (2 + sqrt(12 * N + 4))
          / 6;
 
    // Condition to check if the
    // number is a octagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 8;
 
    // Function call
    if (isoctagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}
                      
                       

Java

// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG {
     
// Function to check if N is a
// octagonal number
public static boolean isoctagonal(int N)
{
    double n = (2 + Math.sqrt(12 * N + 4)) / 6;
     
    // Condition to check if the
    // number is a octagonal number
    return (n - (int)n) == 0;
}
     
// Driver code
public static void main(String[] args)
{
     
    // Given Number
    int N = 8;
     
    // Function call
    if (isoctagonal(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by coder001
                      
                       

Python3

# Python3 program for the above approach
from math import sqrt
 
# Function to check if N is a
# octagonal number
def isoctagonal(N):
 
    n = (2 + sqrt(12 * N + 4)) / 6;
 
    # Condition to check if the
    # number is a octagonal number
    return (n - int(n)) == 0;
     
# Driver Code
if __name__ == "__main__":
 
    # Given number
    N = 8;
 
    # Function call
    if (isoctagonal(N)):
        print("Yes");
     
    else:
        print("No");
 
# This code is contributed by AnkitRai01
                      
                       

C#

// C# program for the above approach
using System;
 
class GFG {
     
// Function to check if N is a
// octagonal number
public static bool isoctagonal(int N)
{
    double n = (2 + Math.Sqrt(12 * N +
                              4)) / 6;
     
    // Condition to check if the
    // number is a octagonal number
    return (n - (int)n) == 0;
}
     
// Driver code
public static void Main(String[] args)
{
     
    // Given number
    int N = 8;
     
    // Function call
    if (isoctagonal(N))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by Rohit_ranjan
                      
                       

Javascript

<script>
 
// JavaScript program for the above approach
 
// Function to check if N is a
// Octagonal Number
function isoctagonal(N)
{
    var n = (2 + Math.sqrt(12 * N + 4)) / 6;
 
    // Condition to check if the
    // number is a octagonal number
    return (n - parseInt(n) == 0);
}
 
// Given Number
var N = 8;
 
// Function call
if (isoctagonal(N)) {
    document.write("Yes");
}
else {
    document.write("No");
}
 
</script>
                      
                       

Output: 
Yes

 

Time Complexity: O(log N) as sqrt function is being used
Auxiliary Space: O(1)



Next Article
Program to check if N is a Octadecagon number
author
spp____
Improve
Article Tags :
  • DSA
  • Mathematical
Practice Tags :
  • Mathematical

Similar Reads

  • Program to check if N is a Pentagonal Number
    Given a number (N), check if it is pentagonal or not. Examples : Input: 12 Output: YesExplanation: 12 is the third pentagonal number Input: 19Output: NoExplanation: The third pentagonal number is 12 while the fourth pentagonal number is 22.Hence 19 is not a pentagonal number. Pentagonal numbers are
    14 min read
  • Program to check if N is a Octadecagon number
    Given a number N, the task is to check if N is an Octadecagon Number or not. If the number N is an Octadecagon Number then print "Yes" else print "No". Octadecagon Number is a 18-sided polygon. The first few Octadecagon Numbers are 1, 18, 51, 100, 165, 246, 343, ... Examples: Input: N = 18 Output: Y
    4 min read
  • Program to check if N is a Centered Octagonal Number
    Given an integer N, the task is to check if it is a Centered Octagonal number or not. If the number N is an Centered Octagonal Number then print "Yes" else print "No". Centered Octagonal number represents an octagon with a dot in the centre and others dots surrounding the centre dot in the successiv
    4 min read
  • Program to check if N is a Pentadecagonal Number
    Given a number N, the task is to check if N is a Pentadecagon Number or not. If the number N is an Pentadecagon Number then print "Yes" else print "No". Pentadecagon Number is a 15-sided polygon..The first few Pentadecagon numbers are 1, 15, 42, 82, 135, 201, ... Examples: Input: N = 15 Output: Yes
    4 min read
  • Program to check if N is a Centered Octadecagonal number
    Given an integer N, the task is to check if it is a Centered Octadecagonal number or not. Print "yes" if it is otherwise output is "no". Centered Octadecagonal number represents a dot in the centre and others dot are arranged around it in successive layers of octadecagon(18 sided polygon). The first
    4 min read
  • Program to check if N is a Centered Pentadecagonal Number
    Given a number N, the task is to check whether N is a Centered pentadecagonal number or not. If the number N is a Centered Pentadecagonal Number then print "Yes" else print "No". Centered Pentadecagonal Number represents a dot in the centre and other dots surrounding it in successive Pentadecagonal(
    4 min read
  • Program to check if N is a Centered Pentagonal Number or not
    Given a number N, the task is to check if N is a Centered Pentagonal Number or not. If the number N is a Centered Pentagonal Number then print "Yes" else print "No". Centered Pentagonal Number is a centered figurate number that represents a pentagon with a dot in the center and other dots surroundin
    4 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
  • Program to check if N is a Star Number
    Given an integer N, the task is to check if it is a star number or not. Star number is a centered figurate number that represents a centered hexagram (six-pointed star) similar to Chinese checker game. The first few Star numbers are 1, 13, 37, 73 ... Examples: Input: N = 13 Output: Yes Explanation:
    4 min read
  • Program to check if N is a Myriagon Number
    Given a number N, the task is to check if N is a Myriagon Number or not. If the number N is an Myriagon Number then print "Yes" else print "No". Myriagon Number is a polygon with 10000 sides. The first few Myriagon numbers are 1, 10000, 29997, 59992, 99985, 149976 ... Examples: Input: N = 10000 Outp
    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