Java Method Exercises: Display the first 50 pentagonal numbers

Java Method: Exercise-7 with Solution

Write a Java method to display the first 50 pentagonal numbers.

A pentagonal number is a figurate number that extends the concept of triangular and square numbers to the pentagon, but, unlike the first two, the patterns involved in the construction of pentagonal numbers are not rotationally symmetrical.

Pictorial Presentation:

Java Method Exercises: Display the first 50 pentagonal numbers

Sample Solution:

Java Code:

import java.util.Scanner;
public class Exercise7 {
public static void main(String[] args) {
   		int count = 1;
		for(int i = 1; i <= 50; i++){
			if(count % 10 == 0) System.out.println();
  public static int getPentagonalNumber(int i) {
		return (i * (3 * i - 1))/2;

Sample Output:

1     5     12    22    35    51    70    92    117   145                                                     
176   210   247   287   330   376   425   477   532   590                                                     
651   715   782   852   925   1001  1080  1162  1247  1335                                                    
1426  1520  1617  1717  1820  1926  2035  2147  2262  2380                                                    
2501  2625  2752  2882  3015  3151  3290  3432  3577  3725


Flowchart: Display the first 50 pentagonal numbers

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Java method to compute the sum of the digits in an integer.
Next: Write a Java method to compute the future investment value at a given interest rate for a specified number of years.

What is the difficulty level of this exercise?

Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH