6-1) (Math: pentagonal numbers) A pentagonal number is defined as n(3n–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 static int getPentagonalNumber(int n) Write a test program that uses this method to display the first n pentagonal numbers with 10 numbers on each line, one space between each number, and the user entering the integer value for n.

Respuesta :

Answer:

Check the explanation

Explanation:

public class Exercise_06_01 {

/** Main Method */

public static void main(String[] args) {

 final int NUMBER_OF_PENTAGONALS = 100; // Display the first 100 pentagonals

 final int NUMBER_PER_LINE = 10; // Display 10 number per line

 // Display the first 100 pentagol numbers

 System.out.println("The first 100 pentagonal numbers, ten per line: ");

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

  // Print ten numbers per line

  if (i % NUMBER_PER_LINE == 0)  

   System.out.printf("%7d\n", getPentagonalNumber(i));

  else

   System.out.printf("%7d", getPentagonalNumber(i));

 }

}

/** Return a pentagonal number */

public static int getPentagonalNumber(int n) {

 return (n * (3 * n - 1)) / 2;

}

}

The pentagonal number is an illustration of loops and conditional statements.

Loops are used to perform repetitive operations, while conditional statements are statements whose execution is dependent on its truth value.

The program in Java where comments are used to explain each line is as follows:

import java.util.*;

public class Main {

   public static void main(String[] args) {

       //This creates a Scanner object

       Scanner input = new Scanner(System.in);

       //This gets input for the count of pentagonal number to print

       int num = input.nextInt();

       //This iterates through num

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

           //This prints on a new line after printing 10 numbers

           if (i % 10 == 0){

               System.out.println(getPentagonalNumber(i));

           }

           else{

               System.out.print(getPentagonalNumber(i)+"\t");

           }

       }

   }

   //This defines the getPentagonalNumber function

   public static int getPentagonalNumber(int n) {

       //This returns the current pentagonal number

       return (n * (3 * n - 1)) / 2;

   }

}

Read more about loops and conditional statements at:

https://brainly.com/question/19344465