SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Factorial calculation

    Hi all, is anyone able to help me out with this problem below pls?

    Basically i want to write a java prog that calculates the factorials of all integers up to 20! and outputs the result in the form:
    1=1
    2*1 =2
    3*2*1=6
    4*3*2*1=24
    ............

    //my piece of code
    class Factorial
    {
    public static void main(String []args)
    {

    int result =1;

    for( int counter = 1; counter <= 20; counter++)

    {

    result = result * counter;
    System.out.println(result);

    }
    }
    }

    the code above ouputs only the results and not the values before the equal sign (ie 3*2*1...) Hows it possible to make the values b4 the equal sign show as well as the results.any help would be appreciated.
    Last edited by fresher; Nov 13, 2006 at 10:28.

  2. #2
    malloc
    Join Date
    Nov 2006
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    ...
    for( int counter = 1; counter <= 20; counter++)
    {
    
    result = result * counter;
    if(counter>1) System.out.print("*");
    System.out.print(counter);
    
    }
    System.out.println("="+result);
    ...
    should do it.

  3. #3
    malloc
    Join Date
    Nov 2006
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, misread your question. Try this instead:

    Code:
    public static void main(String []args)
    {
    
    int result =1;
    StringBuffer sb = new StringBuffer();
    
    for( int counter = 1; counter <= 20; counter++)
    
    {
    
    result = result * counter;
    if(counter>1) sb.append("*");
    sb.append(""+counter);
    System.out.println(sb+"="+result);
    
    }
    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JonSiddle
    Sorry, misread your question. Try this instead:

    Code:
    public static void main(String []args)
    {
    
    int result =1;
    StringBuffer sb = new StringBuffer();
    
    for( int counter = 1; counter <= 20; counter++)
    
    {
    
    result = result * counter;
    if(counter>1) sb.append("*");
    sb.append(""+counter);
    System.out.println(sb+"="+result);
    
    }
    }
    thanks for your solution, the program works correctly but im not clear with a few things as i have started learning java recently.

    First, i cant see where the class type (StringBuffer) of the object (sb) is declared and secondly what is the meaning of sb.append? im guessing it appends '*'. would be great if u could comment the code as well as show the class StringBuffer maybe that will help my understanding. many thanks in advance.

  5. #5
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    StringBuffer is a class that is in the java.lang package. The same package that String lives in. You can read about StringBuffer in the java 1.5 api.
    baby steps... baby steps...

  6. #6
    malloc
    Join Date
    Nov 2006
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First, i cant see where the class type (StringBuffer) of the object (sb) is declared and secondly what is the meaning of sb.append? im guessing it appends '*'. would be great if u could comment the code as well as show the class StringBuffer maybe that will help my understanding. many thanks in advance.
    This line:
    Code:
    StringBuffer sb = new StringBuffer();
    declares the sb object. The StringBuffer class isn't imported explicitly since it's in java.lang (see hooknc's comment).

    A StringBuffer is a mutable String. For various reasons, Strings in java are immutable, which means when you "change" them, you are actually creating a new string, so:

    Code:
    String s = "";
    for(int i=0;i<100;i++) {
    s=s+i;
    }
    would create 101 String objects!

    A StringBuffer lets you do this more efficiently, and
    Code:
    sb.append(aString);
    is equivalent to
    Code:
    s = s + aString;
    Hope this helps


Bookmarks

Posting Permissions

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