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
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;
}
}
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:
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 :)
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;
}
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
Re: Pentagonal Number problem
The getPentagonalNumbers is beeing called but just once, after the loop is end.
I edited my last post with some more information.
Re: Pentagonal Number problem
Thank you very much for your help and input!
Re: Pentagonal Number problem
You'r welcome.
To improve your skills, read this one: Java Methods
Understanding java methods is a very basic and important think that you have to know!
Good luck.