Sunday, October 19, 2025
HomeData Modelling & AIProgram to find Star number

Program to find Star number

A number is termed as star number, if it is a centered figurate number that represents a centered hexagram (six-pointed star) similar to chinese checker game. The few star numbers are 1, 13, 37, 73, 121, 181, 253, 337, 433, ….
Examples: 
 

Input : n = 2
Output : 13

Input : n = 4
Output : 73

Input : n = 6
Output : 181

 

If we take few examples, we can notice that the n-th star number is given by the formula: 
 

n-th star number = 6n(n - 1) + 1 

Below is the implementation of above formula.
 

C++




// C++ program to find star number
#include <bits/stdc++.h>
using namespace std;
  
// Returns n-th star number
int findStarNum(int n)
{
    return (6 * n * (n - 1) + 1);
}
  
// Driver code
int main()
{
    int n = 3;
    cout << findStarNum(n);
    return 0;
}


Java




// Java program to find star number
import java.io.*;
  
class GFG {
    // Returns n-th star number
    static int findStarNum(int n)
    {
        return (6 * n * (n - 1) + 1);
    }
  
    // Driver code
    public static void main(String args[])
    {
        int n = 3;
        System.out.println(findStarNum(n));
    }
}
  
// This code is contributed
// by Nikita Tiwari.


Python3




# Python3 program to
# find star number
  
# Returns n-th 
# star number
def findStarNum(n):
  
    return (6 * n * (n - 1) + 1)
  
# Driver code
n = 3
print(findStarNum(n))
  
# This code is contributed by Smitha Dinesh Semwal


C#




// C# program to find star number
using System;
  
class GFG {
    // Returns n-th star number
    static int findStarNum(int n)
    {
        return (6 * n * (n - 1) + 1);
    }
  
    // Driver code
    public static void Main()
    {
        int n = 3;
        Console.Write(findStarNum(n));
    }
}
  
// This code is contributed
// by vt_m.


PHP




<?php
//PHP program to find star number
  
// Returns n-th star number
function findStarNum($n)
{
    return (6 * $n * ($n - 1) + 1);
}
  
// Driver code
$n = 3;
echo findStarNum($n);
  
// This code is contributed by ajit
?>


Javascript




<script>
// Javascript program to find star number
  
// Returns n-th star number
function findStarNum(n)
{
    return (6 * n * (n - 1) + 1);
}
  
// Driver code
let n = 3;
document.write(findStarNum(n));
  
// This code is contributed by rishavmahato348.
</script>


Output : 
 

37

Time complexity: O(1) since performing constant operations

Space complexity: O(1) since using constant variables

Interesting Properties of Start Numbers: 
 

  1. The digital root of a star number is always 1 or 4, and progresses in the sequence 1, 4, 1.
  2. The last two digits of a star number in base 10 are always 01, 13, 21, 33, 37, 41, 53, 61, 73, 81, or 93.
  3. The generating function for the star numbers is 
     
x*(x^2 + 10*x + 1) / (1-x)^3 = x + 13*x^2 + 37*x^3 +73*x^4 .......
  1. The star numbers satisfy the linear recurrence equation 
     
S(n) = S(n-1) + 12(n-1)

References : 
http://mathworld.wolfram.com/StarNumber.html 
https://en.wikipedia.org/wiki/Star_number
This article is contributed by DANISH_RAZA . If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS