
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program for centered nonagonal number in C++
Given with a value ‘n’ and the task is to generate the centered nonagonal number for n and centered nonagonal series till n and display the results.
What is centered nonagonal number?
Centered nonagonal number contains the nonagonal layers formed by the dots and one corresponding dot in the center.
Given above is the figure of centered nonagonal number š2.It can be calculated using the formula −
$$Nc(n)=\frac{(3n-2)(3n-1)}{2}$$
Input
number: 20
Output
centered nonagonal number : 1711
Input
number: 10
Output
centered nonagonal series : 1 10 28 55 91 136 190 253 325 406
Algorithm
Start Step 1→ declare function to calculate centered nonagonal number int calculate_number(int num) return (3 * num - 2) * (3 * num - 1) / 2 Step 2→ declare function to calculate centered nonagonal series int calculate_series(int num) Loop For int i = 1and i <= num and i++ Print (3 * i - 2) * (3 * i - 1) / 2 End Step 3→ In main() Declare int num = 20 Call calculate_number(num) Declare num = 10 Call calculate_series(num) Stop
Example
#include <bits/stdc++.h> using namespace std; //calculate centered nonagonal number int calculate_number(int num){ return (3 * num - 2) * (3 * num - 1) / 2; } int calculate_series(int num){ for (int i = 1; i <= num; i++){ cout << (3 * i - 2) * (3 * i - 1) / 2; cout << " "; } } int main(){ int num = 20; cout<<"centered nonagonal number : "<<calculate_number(num)<<endl; num = 10; cout<<"centered nonagonal series : "; calculate_series(num); return 0; }
Output
If run the above code it will generate the following output −
centered nonagonal number : 1711 centered nonagonal series : 1 10 28 55 91 136 190 253 325 406
- Related Articles
- Program for Centered Icosahedral Number in C++
- Centered Dodecagonal Number
- Centered Hexadecagonal Number
- Centered Tridecagonal Number
- Recursive program for prime number in C++
- Program for factorial of a number in C program
- C/C++ Program for nth Catalan Number?
- C/C++ Program for Triangular Matchstick Number?
- C Program for nth Catalan Number
- C/C++ Program for the Triangular Matchstick Number?
- C Program for n-th even number
- C Program for n-th odd number
- Program for Number of solutions to Modular Equations in C/C++?
- C/C++ Program for Finding the Number Occurring Odd Number of Times?
- C/C++ Program for the n-th Fibonacci number?

Advertisements