Results 1 to 6 of 6
Thread: Pentagonal Number problem
- 09-07-2011, 03:05 AM #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; } }
- 09-07-2011, 04:50 AM #2
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 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:
edit:Java Code:int x; x = getPentagonalNumber(100);
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.
- 09-07-2011, 04:57 AM #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
- 09-07-2011, 05:03 AM #4
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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.
- 09-07-2011, 06:03 AM #5
Member
- Join Date
- Sep 2011
- Posts
- 23
- Rep Power
- 0
Re: Pentagonal Number problem
Thank you very much for your help and input!
- 09-07-2011, 06:19 AM #6
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
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.
Similar Threads
-
Phone number stringtokenizer problem
By jacques5309 in forum New To JavaReplies: 10Last Post: 11-14-2011, 02:12 AM -
number problem
By click66 in forum New To JavaReplies: 7Last Post: 10-11-2010, 03:27 PM -
Math: pentagonal numbers
By Latanyar in forum New To JavaReplies: 7Last Post: 10-10-2010, 03:14 PM -
Printing the Number of Times a Number in a Range Shows up
By space4rent00 in forum New To JavaReplies: 1Last Post: 02-05-2010, 10:42 PM -
[SOLVED] Problem hadling number(money) > 214,74,83,647.99
By playwin2 in forum New To JavaReplies: 2Last Post: 10-14-2008, 09:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks