Results 1 to 6 of 6
  1. #1
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default 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;
             
           
            
            
            
            
            
            
        }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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.

  3. #3
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default 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

  4. #4
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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.

  5. #5
    Jason is offline Member
    Join Date
    Sep 2011
    Posts
    23
    Rep Power
    0

    Default Re: Pentagonal Number problem

    Thank you very much for your help and input!

  6. #6
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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

  1. Phone number stringtokenizer problem
    By jacques5309 in forum New To Java
    Replies: 10
    Last Post: 11-14-2011, 02:12 AM
  2. number problem
    By click66 in forum New To Java
    Replies: 7
    Last Post: 10-11-2010, 03:27 PM
  3. Math: pentagonal numbers
    By Latanyar in forum New To Java
    Replies: 7
    Last Post: 10-10-2010, 03:14 PM
  4. Printing the Number of Times a Number in a Range Shows up
    By space4rent00 in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 10:42 PM
  5. Replies: 2
    Last Post: 10-14-2008, 09:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •