-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPentagonalNumbers.java
More file actions
28 lines (24 loc) · 1005 Bytes
/
Copy pathPentagonalNumbers.java
File metadata and controls
28 lines (24 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* Exercise 6.1
* (Math: pentagonal numbers) A pentagonal number is defined as n(3*n-1)/2 for
* n = 1, 2, . . ., and so on. Therefore, the first few numbers are 1, 5, 12, 22, . . . .
* Write a method with the following header that returns a pentagonal number
*/
public class PentagonalNumbers {
public static void main(String[] args) {
// Limit printing to 10 pentagonal numbers on each line
final int PENTAGONALS_PER_LINE = 10;
// Display pentagonal numbers from 1 to 100, based on the Pentagonal method
System.out.println("Pentagonal numbers from 1 to 100 are: ");
for (int i = 1 ; i <= 100; i++) {
if (i % PENTAGONALS_PER_LINE == 0 )
// Left aligning result in columns of 6 decimal integers including spcae
System.out.printf( "%-6d\n", getPentagonalNumber(i));
else
System.out.printf( "%-6d", getPentagonalNumber(i));
}
}
// Pentagonal method
public static int getPentagonalNumber(int n) {
return (n * (3 * n - 1)) / 2;
}
}