Decagonal Number:
A decagonal number is a figurate number that extends the triangle and square number concepts to the decagon (a ten-sided polygon). The nth decagonal number counts the number of dots in a pattern of n nested decagons, each of which has a shared corner.
Formula to generate a Decagonal Number:
D(n) = 4*n^2 - 3*n
Given a number and the task is to find the decagonal number of a given number.
Examples:
Example1:
Input:
Given Number = 4
Output:
The decagonal number of a given number { 4 } =
52Example2:
Input:
Given Number = 10
Output:
The decagonal number of a given number { 10 } =
370Program to Find nth Decagonal Number in Python
Method #1: Using Mathematical Formula (Static Input)
Approach:
- Create a function say Find_DecagonalNum() which accepts the given number as an argument and returns the decagonal number of the given number.
- Inside the function, calculate the decagonal number of a given number using the above given mathematical formula and return it.
- Give the number as static input and store it in a variable.
- Pass the given number to the Find_DecagonalNum() function store it in another variable.
- Print the decagonal number of a given number.
- The Exit of the Program.
Below is the implementation:
# Create a function say Find_DecagonalNum() which accepts the given number as an
# argument and returns the decagonal number of the given number.
def Find_DecagonalNum(gvn_num):
# Inside the function, calculate the decagonal number of a given number using the
# above given mathematical formula and return it.
return(4*gvn_num*gvn_num - 3*gvn_num)
# Give the number as static input and store it in a variable.
gvn_num = 4
# Pass the given number to the Find_DecagonalNum() function store it in another
# variable.
rslt = Find_DecagonalNum(gvn_num)
# Print the decagonal number of a given number.
print("The decagonal number of a given number {", gvn_num, "} = ")
print(rslt)
Output:
The decagonal number of a given number { 4 } =
52Method #2: Using Mathematical Formula (User Input)
Approach:
- Create a function say Find_DecagonalNum() which accepts the given number as an argument and returns the decagonal number of the given number.
- Inside the function, calculate the decagonal number of a given number using the above given mathematical formula and return it.
- Give the number as user input using the int(input()) function and store it in a variable.
- Pass the given number to the Find_DecagonalNum() function store it in another variable.
- Print the decagonal number of a given number.
- The Exit of the Program.
Below is the implementation:
# Create a function say Find_DecagonalNum() which accepts the given number as an
# argument and returns the decagonal number of the given number.
def Find_DecagonalNum(gvn_num):
# Inside the function, calculate the decagonal number of a given number using the
# above given mathematical formula and return it.
return(4*gvn_num*gvn_num - 3*gvn_num)
# Give the number as user input using the int(input()) function and store it in a variable.
gvn_num = int(input("Enter some random number = "))
# Pass the given number to the Find_DecagonalNum() function store it in another
# variable.
rslt = Find_DecagonalNum(gvn_num)
# Print the decagonal number of a given number.
print("The decagonal number of a given number {", gvn_num, "} = ")
print(rslt)
Output:
Enter some random number = 10
The decagonal number of a given number { 10 } =
370