1. Member Join Date
Sep 2011
Posts
23
Rep Power
0

## Pentagonal Number problem

Hi guys, i am having a problem with a homework assignment that i would appreciate some help with if possible, i understand it is my homework and i want to learn to program as a career so i am in no way asking for a blatent answer. I have to crate a method public static int getPentagonalNumber(int n) and generate 10 lines of 10 pentagonal numbers 1-9 on the first line and so on until 100 numbers are generated, the formula is n*(3*n-1)/2, this is what i have so far and i am stumped and have really made this more confusing then i think it is

Java Code:
```package chapter_5;

/**
*
* @author jason
*/
public class Five_One {

public static void main(String[] args) {
int n = 0;
int numPerLine = 10;
int formula = n * (3 * n - 1) / 2;
for (n = 0; n<11; n++)
System.out.println("" +n);
getPentagonalNumber(100);

}

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

}

}```  Reply With Quote

2. Senior Member Join Date
Aug 2011
Posts
251
Rep Power
9

## Re: Pentagonal Number problem

I didn't understood what do you want to do, but here is some things you should know:
In your while statement you didn't used brackets so only the line after the while condition is in the loop.
And the most important thing is that the returning value from the "getPentagonalNumber" is going no where.
You should do somthing like:
Java Code:
```int x;
x = getPentagonalNumber(100);```
edit:
I think I got it,
you are trying to send 100 numbers to your method and print it on 10 lines, 10 values each line?
if yes here is my answer:
For displaying somthing like that you need a Nested loop (loop inside a loop) to display each line.
We will send the index number of the nested loop each time of course and print it.
Here is the code example, if you dont wanna see it then just ignore it :)
Java Code:
```public static void main(String[]args)
{
for (int k=1;k<10;k++)
{
for (int n=1;n<10;n++)
{
System.out.print(getPentagonalNumber(n)+"\t");
}
System.out.println();
}

}
public static int getPentagonalNumber(int n)
{
return  n * (3 * n - 1) / 2;
}```
Last edited by tnrh1; 09-07-2011 at 05:05 AM.  Reply With Quote

3. Member Join Date
Sep 2011
Posts
23
Rep Power
0

## Re: Pentagonal Number problem

I see , so since there are no brackets the return getPentagonalNumbers is not being called which will not equate the problem, in the end I am trying to get that equation to run 10 times , each time using n, n=1,n=2 and so on until it has ran ten times and printed out 100 numbers , 10 lines of 10, my biggest problem has been figuring out ow to add numerical value to n  Reply With Quote

4. Senior Member Join Date
Aug 2011
Posts
251
Rep Power
9

## Re: Pentagonal Number problem

The getPentagonalNumbers is beeing called but just once, after the loop is end.  Reply With Quote

5. Member Join Date
Sep 2011
Posts
23
Rep Power
0

## Re: Pentagonal Number problem

Thank you very much for your help and input!  Reply With Quote

6. Senior Member Join Date
Aug 2011
Posts
251
Rep Power
9

## Re: Pentagonal Number problem

You'r welcome.  Reply With Quote