Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In mathematics, a Hexagonal number is a number that represents the number of dot

ID: 3803659 • Letter: I

Question

In mathematics, a Hexagonal number is a number that represents the number of dots needed to form a hexagon. Starting with 1 (the first point) we can add the appropriate number of dots so that the result is a hexagon of increasing size.

Based on the example above, the first few hexagonal numbers are:

1, 6, 15, 28, 45, 66, 91, 120, 153, 190, 231, 276, 325, 378, …

To get a number in the hexagonal sequence, we use the formula pn= n(2n-1) for n>=1.

Write a program that does the following:

Prompts the user for a starting and ending integer value

Checks the values so that:

The starting value is greater than 0

The ending value is greater than the starting value

Calls a method called getHexagonalNumber to calculate and return the corresponding Hexagonal Number

Prints the number and its corresponding hexagonal value for the given range (in a table format)

Samples of the output are shown below. (Note, your program does not have to have the exact wording in the output, but should accomplish the same tasks.)

Sample Output 1

This program will print a table of hexagonal numbers in a given range.
Enter a starting value (greater than 0): 0
Your previous entry is invalid.
Enter another starting value: 1
Enter an ending value (greater than your starting value): -9
Your previous entry is invalid.
Enter another ending value: 10

Number       Hexagonal Number
-----------------------------------
   1             1
   2             6
   3             15
   4             28
   5             45
   6             66
   7             91
   8             120
   9             153
   10 190

Sample Output 2

This program will print a table of hexagonal numbers in a given range.
Enter a starting value (greater than 0): 15
Enter an ending value (greater than your starting value): 9
Your previous entry is invalid.
Enter another ending value: 25

Number       Hexagonal Number
-----------------------------------
   15             435
   16             496
   17             561
   18             630
   19             703
   20             780
   21             861
   22             946
   23            1035
   24             1128
   25             1225

Explanation / Answer

import java.util.*;
import java.io.*;
class HexagonalNumber
{
public static void getHexagonalNumber(int strt,int end){

int n,result;

System.out.println("Number HexagonalNumber ------------------------------------");

for(n=strt;n<=end;n++){

result=n*(2*n-1);

System.out.println(n+" "+result);

}
}
public static void main(String[] args)
    {
        int strt,end ;
  Scanner sc=new Scanner(System.in);
System.out.println("This program will print a table of hexagonal numbers in a given range! Enter a staring value(greater than 0):");
strt=sc.nextInt(); //loop till user end valid entry
do{

   if(strt<=0){

       System.out.println("Your previous entry is invalid. Enter another starting value : ");

       strt=sc.nextInt();
    }

}while(strt<=0);

System.out.println("Enter a ending value(greater than your starting value):");

end =sc.nextInt();

do{

   if(end<=strt){

       System.out.println("Your previous entry is invalid. Enter another ending value :");
        end=sc.nextInt();
    }

}while(end<strt);

getHexagonalNumber(strt,end);

    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote