Given a number n, the task is to find nth Centered heptagonal number. Centered heptagonal number is centered figure number that represents a heptagon with dot in center and all other dot surrounding in heptagonal form. Nth centered heptagonal number can be calculated by using formula (7n2 – 7n + 2) / 2. Examples :
Input : n = 2
Output : 8
Input : n = 7
Output : 148
Please refer this diagram for pictorial representation.
Below is the implementation :
C++
// CPP program to find n-th
// Centered heptagonal number
#include <bits/stdc++.h>
usingnamespacestd;
// Function to find Centered
// heptagonal number
intcentered_heptagonal_num(longintn)
{
// Formula to calculate nth
// Centered heptagonal number
return(7 * n * n - 7 * n + 2) / 2;
}
// Driver Code
intmain()
{
longintn = 5;
cout << n << "th Centered heptagonal number : ";
cout << centered_heptagonal_num(n);
return0;
}
C
// C program to find n-th
// Centered heptagonal number
#include <stdio.h>
// Function to find Centered
// heptagonal number
intcentered_heptagonal_num(longintn)
{
// Formula to calculate nth
// Centered heptagonal number
return(7 * n * n - 7 * n + 2) / 2;
}
// Driver Code
intmain()
{
longintn = 5;
printf("%ldth Centered heptagonal number : ",n);
printf("%d",centered_heptagonal_num(n));
return0;
}
// This code is contributed by kothavvsaakash.
Java
// Java program to find n-th Centered
// heptagonal number
importjava.io.*;
classGFG {
// Function to find Centered heptagonal
// number
staticlongcentered_heptagonal_num(longn)
{
// Formula to calculate nth
// Centered heptagonal number
return(7* n * n - 7* n + 2) / 2;
}
// Driver Code
publicstaticvoidmain (String[] args)
{
longn = 5;
System.out.println( n + "th Centered "
+ "heptagonal number : "
+ centered_heptagonal_num(n));
}
}
// This code is contributed by anuj_67.
Python3
# Python program to find nth
# Centered heptagonal number
# Function to find Centered
# heptagonal number
defcentered_heptagonal_num(n):
# Formula to calculate nth
# Centered heptagonal number
return(7*n *n -7*n +2) //2
# Driver Code
n =5
print("%sth Centered heptagonal number : "%n,
centered_heptagonal_num(n))
C#
//C# program to find n-th Centered
// heptagonal number
usingSystem;
classGFG {
// Function to find Centered heptagonal
// number
staticlongcentered_heptagonal_num(longn)
{
// Formula to calculate nth
// Centered heptagonal number
return(7 * n * n - 7 * n + 2) / 2;
}
// Driver Code
publicstaticvoidMain ()
{
longn = 5;
Console.WriteLine( n + "th Centered "
+ "heptagonal number : "
+ centered_heptagonal_num(n));
}
}
// This code is contributed by anuj_67.
PHP
<?php
// PHP program to find n-th
// Centered heptagonal number
// Function to find Centered
// heptagonal number
functioncentered_heptagonal_num($n)
{
// Formula to calculate nth
// Centered heptagonal number
return(7 * $n* $n- 7 *
$n+ 2) / 2;
}
// Driver Code
$n= 5;
echo$n,"th Centered heptagonal number : ";
echocentered_heptagonal_num($n);
// This code is contributed by m_kit
?>
Javascript
<script>
// Javascript program to find n-th
// Centered heptagonal number
// Function to find Centered
// heptagonal number
functioncentered_heptagonal_num(n)
{
// Formula to calculate nth
// Centered heptagonal number
returnparseInt((7 * n * n - 7 * n + 2) / 2);
}
// Driver Code
let n = 5;
document.write(n + "th Centered heptagonal number : ");
document.write(centered_heptagonal_num(n));
// This code is contributed by rishavmahato348.
</script>
Output :
5th Centered heptagonal number : 71
Time Complexity: O(1) Auxiliary Space: O(1)
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!