Java Arithmetic Operator calculate pentagonal numbers

Question

A pentagonal number is defined as n(3n-1)/2 for n = 1, 2, . . ., and so on.

The first few numbers are 1, 5, 12, 22, . . . .

We would like to write a method with the following header that returns a pentagonal number:

public static int getPentagonalNumber(int n) 

public class Main {

    public static void main(String[] args) {

        for (int i = 1; i <= 100; i++) {

            System.out.printf("%10s",(i % 8 != 0) ? getPentagonalNumber(i) + " " : getPentagonalNumber(i) + "\n");

        }//  ww w  .  ja va 2 s. c o  m
    }

    public static int getPentagonalNumber(int n) {
       //your code here
    }
}





public class Main {

    public static void main(String[] args) {

        for (int i = 1; i <= 100; i++) {

            System.out.printf("%10s",(i % 8 != 0) ? getPentagonalNumber(i) + " " : getPentagonalNumber(i) + "\n");

        }
    }

    public static int getPentagonalNumber(int n) {
        return n * (3 * n - 1) / 2;
    }
}



PreviousNext

Related