Star Number in C++22 May 2025 | 7 min read IntroductionA "star number" refers to a type of figurate number that represents a centered hexagram, a six-pointed star. These numbers are part of a broader category of numbers that visually form geometric patterns. The nth star number can be calculated using a specific formula, and this concept finds applications in mathematical visualizations and combinatorics. This article provides an understanding of star numbers, a mathematical formula to compute them, and an implementation in C++ to calculate and display the star numbers. Problem Statement:Given an integer n, compute the nth star number using its formula: Star number=6n(n−1) +1 Write a C++ program that:
Understanding the Formula:The formula 6n(n−1)+1 works as follows:
For example:
Algorithm:
Example 1:Let us take an example to illustrate the Star Number in C++. Output: Enter the value of n to calculate the nth star number: 4 The 4th star number is: 73 Example 2:Let us take another example to illustrate the Star Number in C++. Output: ==== Star Number Calculator ==== 1. Calculate a single star number 2. Calculate and display multiple star numbers 3. Save star numbers to a file 4. Exit Enter your choice: 1 Enter the value of n to calculate the nth star number: 5 The 5th star number is: 121 ==== Star Number Calculator ==== 1. Calculate a single star number 2. Calculate and display multiple star numbers 3. Save star numbers to a file 4. Exit Enter your choice: 2 Enter the maximum value of n to calculate star numbers up to nth: 8 Star Numbers up to 8: n Star Number ------------------------- 1 1 2 13 3 37 4 73 5 121 6 181 7 253 8 337 ==== Star Number Calculator ==== 1. Calculate a single star number 2. Calculate and display multiple star numbers 3. Save star numbers to a file 4. Exit Enter your choice: 3 Enter the maximum value of n to calculate star numbers up to nth: 2 Star numbers have been saved to 'StarNumbers.txt'. ==== Star Number Calculator ==== 1. Calculate a single star number 2. Calculate and display multiple star numbers 3. Save star numbers to a file 4. Exit Enter your choice: 4 Exiting program. Goodbye! Explanation:
Applications of Star Number:Several applications of the Star Number in C++ are as follows:
Conclusion:In conclusion, Star numbers are an intriguing mathematical concept with simple computational requirements. Using C++, this article demonstrates how to compute and display the nth star number effectively. With minor modifications, this program can be extended for more complex applications involving geometric patterns or sequences. Next TopicStrobogrammatic-number-in-cpp |
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India