Results 1 to 8 of 8
  1. #1
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default Math: pentagonal numbers

    I am writing a method that returns a pentagonal number.

    public static int getPentagonalNumber(int n)

    I have to write a test program that displays the first 100 pentagonal numbers with 10 numbers on each line.

    This is what I have so far.

    public class getPentagonalNumber
    {
    public static void main(String[] args)
    {
    int n;
    int getPentagonalNumber = ((n) *(3n - 1)(/2));
    for(n=1; n >=100; n++)
    {
    System.out.println(n(1, 100));
    System.out.println();
    }
    }
    public static int getPentagonalNumber(int n)
    {
    return getPentagonalNumber;

    }
    }

    I'm not sure if this is the correct format on starting the method. Could anyone guide me into the right direction?

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    uiuiui, no no..
    you have to move the formula in the method.(the formula has some syntax error e.g. 3n)
    in your loop you have to call this method with i as the paramter. and you must print a linebreak if "i" modulo 10 is equal 0

    (and rename your class :D )
    Last edited by eRaaaa; 10-09-2010 at 10:52 PM. Reason: 10 numbers on each line not 5, sorry :)

  3. #3
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by eRaaaa View Post
    uiuiui, no no..
    you have to move the formula in the method.(the formula has some syntax error e.g. 3n)
    in your loop you have to call this method with i as the paramter. and you must print a linebreak if "i" modulo 10 is equal 0

    (and rename your class :D )

    public class getPentagonalNumber
    {
    public static void main(String[] args)
    {
    int n;

    for(n=1; n >=100; n++)
    {
    System.out.println(n(1, 100));
    System.out.println();
    }
    }
    public static int getPentagonalNumber(int n)
    {
    int getPentagonalNumber = n *(3*n - 1)/2;
    return getPentagonalNumber;

    }
    }

    Ok. I changed (3n to (3*n-1)/2 but System.out.println(n(1, 100));
    is saying cannot find symbol method. What should I rename my class?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    786
    Rep Power
    5

    Default

    ....
    first: for(n=1; n >=100; n++) - you will never enter the loop...--> n<=100
    second: System.out.println(n(1, 100)); - :confused: i dont know what are you trying there :) - call the method !!

    Java Code:
    public class PentagonalNumber {
    
    	public static void main(String[] args) {
    		for (int i = 1; i <= 100; i++) {
    			//System.out.print(getPentagonalNumber(i) + (i %10 == 0? System.getProperty("line.separator") : "\t")); //advanced ;D
    			System.out.print(getPentagonalNumber(i));
    			if (i % 10 == 0) {
    				System.out.println();
    			} else {
    				System.out.print("\t");
    			}
    		}
    	}
    
    	public static int getPentagonalNumber(int n) {
    		return (3 * n * n - n) / 2;
    	}
    }

  5. #5
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

    Default

    Thank you, but I am still confused at the point where

    if (i % 10 == 0) {
    System.out.println();
    } else {
    System.out.print("\t");
    }
    }
    }
    I guess I am trying to grasp the concept on methods. We just started on methods and I am really confused. Also, could I use n instead of i, because it says n*(3*n-1)/2 for n = 1, 2....and so on. So could mines get put in this way:

    public class getPentagonalNumber
    {
    public static void main(String[] args)
    {
    int n;

    for(n=1; n <=100; n++)
    {
    System.out.print(getPentagonalNumber(n));
    if (n % 10 == 0)
    {
    System.out.println();
    }
    else
    {
    System.out.print("\t");
    }
    }
    }



    public static int getPentagonalNumber(int n)
    {

    return n *(3*n - 1)/2;

    }
    }

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Latanyar View Post
    I am writing a method that returns a pentagonal number.

    public static int getPentagonalNumber(int n)

    I have to write a test program that displays the first 100 pentagonal numbers with 10 numbers on each line.

    This is what I have so far.

    public class getPentagonalNumber
    {
    public static void main(String[] args)
    {
    int n;
    int getPentagonalNumber = ((n) *(3n - 1)(/2));
    for(n=1; n >=100; n++)
    {
    System.out.println(n(1, 100));
    System.out.println();
    }
    }
    public static int getPentagonalNumber(int n)
    {
    return getPentagonalNumber;

    }
    }

    I'm not sure if this is the correct format on starting the method. Could anyone guide me into the right direction?
    int getPentagonalNumber = ((n) *(3n - 1)(/2));

    n has not been assigned and you cannot do 3n, it means nothing to java.

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Quote Originally Posted by eRaaaa View Post
    ....
    first: for(n=1; n >=100; n++) - you will never enter the loop...--> n<=100
    second: System.out.println(n(1, 100)); - :confused: i dont know what are you trying there :) - call the method !!

    Java Code:
    ...
    Please avoid spoon-feeding the OP as he will learn nothing this way. Simply providing a solution is unhelpful and does more harm than good, as when he comes across a test environment he won't have that kind of help.

    @ OP, that part of the code that you don't get puts ten numbers on each line. The print() function prints out text without a newline, then the println() function prints out that text WITH a newline, as such:
    Java Code:
    System.out.println("a");
    System.out.print("b");
    System.out.print("c");
    System.out.println("d");
    System.out.print("e");
    System.out.println();
    System.out.print("f");
    This code will produce:
    Java Code:
    abcd
    e
    f
    Play around with both print() and println() to see what you can do.

    Also, if you want to understand the point of the n%10 part, look up some stuff on the modulus operator: The Remainder or Modulus Operator in Java

    Good luck!

  8. #8
    Latanyar is offline Member
    Join Date
    Oct 2010
    Posts
    25
    Rep Power
    0

Similar Threads

  1. Roundind up numbers using Math.ceil
    By Allspark in forum New To Java
    Replies: 1
    Last Post: 09-08-2010, 05:15 AM
  2. Math.random
    By p0rnstar in forum New To Java
    Replies: 9
    Last Post: 01-27-2010, 01:26 AM
  3. Math Program
    By stlboi in forum New To Java
    Replies: 8
    Last Post: 04-02-2009, 09:34 PM
  4. Anyone good at math?
    By iWonder in forum Forum Lobby
    Replies: 12
    Last Post: 01-11-2009, 04:12 PM
  5. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 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
  •