# Factorial

Page 1 of 1

## 5 Replies - 868 Views - Last Post: 08 July 2010 - 02:27 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=180624&amp;s=6712ff6c80f621073c8a5a2d0dc18b39&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sh1n3

Reputation: 24
• Posts: 164
• Joined: 22-April 10

# Factorial

Posted 07 July 2010 - 01:49 PM

```Factorial.java:19: 'void' type not allowed here
return n*fact(n-1);
^
1 error
Press any key to continue . . .

```

That is the error message that I am getting. It's the first time I've ever seen anything like it!
The code is right below.

```import java.io.BufferedReader;
import java.io.IOException;
class Factorial {
public static void fact(int x) {
int f=1;
for (int i=1;i<x;i++){
f*=i;
}
System.out.println(f);
}
public static int factr(int n) {
if (n==1)
{
return n;
}
else
{
return n*fact(n-1);
}
}
public static void main(String[] args)throws IOException {
System.out.print("Enter a Number : ");
fact(input);
int output=factr(input);
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Factorial

### #2 bcranger

• D.I.C Lover

Reputation: 252
• Posts: 1,199
• Joined: 01-February 10

## Re: Factorial

Posted 07 July 2010 - 01:54 PM

Your problem is that you are trying to return n * fact

fact is of type void...you need a number type

you declared the fact method as void so it doenst return anything

### #3 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

## Re: Factorial

Posted 07 July 2010 - 01:55 PM

On that line, you want to call factr, not fact.

```return n*factr(n-1);
```

That should do it.

### #4 sh1n3

Reputation: 24
• Posts: 164
• Joined: 22-April 10

## Re: Factorial

Posted 07 July 2010 - 02:05 PM

WOW solved, thanks....problems of a letter!!

### #5 Dogstopper

Reputation: 2920
• Posts: 11,194
• Joined: 15-July 08

## Re: Factorial

Posted 07 July 2010 - 02:39 PM

yah fact() is your iterative solution. factr() is your recursive solution. Both should be independent and not reference the other as Locke pointed out.

### #6 jeremejazz

Reputation: 22
• Posts: 48
• Joined: 23-April 10

## Re: Factorial

Posted 08 July 2010 - 02:27 AM

good!