58.4k views
4 votes
A pentagonal number is defined as

(n(3n1))/2
for n = 1; 2; ::: and so on. So, the first few pentagonal numbers are 1; 5; 12; 22; :::

Write the following method that returns the nth pentagonal number:
public static int getPentagonalNumber( int n )

User Jens Wegar
by
4.9k points

1 Answer

0 votes

Answer:

public static int getPentagonalNumber( int n ){

//initialize the variable

int NthpentagonalNumber;

// use formula

NthpentagonalNumber = n*((3*n)-1)/2;

//return the result

return NthpentagonalNumber;

}

Step-by-step explanation:

Create the function with return type int. it means, the function returns the integer value to the calling function.

In the function, we declare the one parameter with an integer type. so, it takes the value from the calling function.

then, initialize the variable for storing the output.

the formula for calculating the pentagonal number is:


pentagonal number = n*(3n-1)/2

the value is passed by the user and the function capture the value in the parameter and then use the formula to store the output the variable.

and finally, return the output.

User Jamal Zare
by
4.5k points