You are given a number n, the task is to find the nth Decagonal number. A decagonal number is a figurate number that extends the concept of triangular and square numbers to the decagon (a ten-sided polygon). The nth decagonal numbers counts the number of dots in a pattern of n nested decagons, all sharing a common corner, where the ith decagon in the pattern has sides made of i dots spaced one unit apart from each other.
Examples:
Input : n = 3
Output : 27
Input : n = 7
Output : 175
The n-th decagonal number is given by the formula
(4n 2 - 3n).
C++
// C++ program to find nth decagonal number
#include <bits/stdc++.h>
using namespace std;
// Function to calculate decagonal number
int decagonal(int n)
{
// Formula for finding nth decagonal number
return 4 * n * n - 3 * n;
}
// Driver function
int main()
{
int n = 10;
cout << n << "th decagonal number :" << decagonal(n);
return 0;
}
Java
// JAVA Code for Decagonal Numbers
import java.util.*;
class GFG {
// Function to calculate
// decagonal number
static int decagonal(int n)
{
// Formula for finding nth
// decagonal number
return 4 * n * n - 3 * n;
}
/* Driver function */
public static void main(String[] args)
{
int n = 10;
System.out.println(n + "th decagonal number :"
+ decagonal(n));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python
# Python program to find nth decagonal number
def decagonal(n):
return 4 * n * n - 3 * n
# Driver code
n = 10
print(n, "th decagonal number :", decagonal(n))
C#
// C# Code for Decagonal Numbers
using System;
class GFG {
// Function to calculate
// decagonal number
static int decagonal(int n)
{
// Formula for finding nth
// decagonal number
return 4 * n * n - 3 * n;
}
// Driver Code
public static void Main()
{
int n = 10;
Console.Write(n + "th decagonal number : "
+ decagonal(n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to find nth
// decagonal number
// Function to calculate
// decagonal number
function decagonal($n)
{
// Formula for finding nth
// decagonal number
return 4 * $n * $n - 3 * $n;
}
// Driver function
$n = 10;
echo $n, "th decagonal number :",
decagonal($n);
// This code is contributed by ajit
?>
JavaScript
<script>
// JavaScript program for Decagonal Numbers
// Function to calculate
// decagonal number
function decagonal(n)
{
// Formula for finding nth
// decagonal number
return 4 * n * n - 3 * n;
}
// Driver code
let n = 10;
document.write(n + "th decagonal number : " +
decagonal(n));
// This code is contributed by souravghosh0416
</script>
Output:
10th decagonal number : 370
Time Complexity: O(1)
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem