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 )

Respuesta :

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;

   }

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:

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

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.