1. ## 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.

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

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. Originally Posted by JonSiddle

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

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

#### Posting Permissions

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