# Missing Return Statement in code to display factorials from 1-100

Page 1 of 1

## 6 Replies - 932 Views - Last Post: 21 March 2012 - 09:33 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=271783&amp;s=c2d550d95300064676ce401fc7211951&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hambonie

Reputation: 0
• Posts: 34
• Joined: 13-March 12

# Missing Return Statement in code to display factorials from 1-100

Posted 20 March 2012 - 01:28 PM

I was given this assignment last week in an intro to java course:
Print out the factorials from 1-100. First use integers. What happens? and why? Then use BigInteger... I had it working using the following code but my prof is being picky and wants me to do it another way :{
```import javax.swing.*;

public class FactorialMethod {
public static void main(String[] args) {
long start = 0;
System.out.println("The factorials from 1-100 are:");
factorial(start);
}

public static void factorial(long start) {
int counter = 1;
double factorial = 0;
int x;
while (counter<=100){
System.out.print("Factorial of " + counter + " is");
factorial = counter/counter;
for (x=counter; x>0; x--){
factorial = factorial*x;
}
System.out.print(" " + Math.abs(factorial) + "\n\n");
counter++;
}
}
}
```

as i said the above logic was not acceptable; so, i started from scratch and have come up with the following. unfortunately there is an error in my factorial method relating to a missing return statement. Does anyone have any ideas?
```import java.util.*;
import java.math.*;
public class FactorialMethod2 {

public static void main(String[] args) {

int counter = 1;
int start = 0;
System.out.println("Factorials from 1-100 are: ");
factorial(counter);
factorial(start);

}

//return factorials for 1-100
public static int factorial(int counter){

counter = 1;
int factorial = 0;
int x;
while (counter<=100){
System.out.print("Factorial of " + counter + " is");
factorial = counter/counter;
for (x=counter; x>0; x--){
factorial = factorial*x;

if (counter == 0)
return 1;
else
return counter * factorial( counter-1 ); //recursive call

}
}
}
}
```

Is This A Good Question/Topic? 0

## Replies To: Missing Return Statement in code to display factorials from 1-100

### #2 blackcompe

• D.I.C Lover

Reputation: 1156
• Posts: 2,538
• Joined: 05-May 05

## Re: Missing Return Statement in code to display factorials from 1-100

Posted 20 March 2012 - 01:53 PM

Quote

as i said the above logic was not acceptable; so, i started from scratch and have come up with the following. unfortunately there is an error in my factorial method relating to a missing return statement. Does anyone have any ideas?

There's a more elegant way to write a recursive factorial function. Two parts:

• A base case : The condition which stops the recursion
• A recursive call : Do the same operation but with a different input.

1 + 2 + 3 + 4 ... : That pattern is similar to a factorial. f(n) = n + f(n-1), for n > 0

You're currently using an iterative approach. Why don't you work on that.

Factorial growth is greater than exponential growth, so for increasing inputs the output will grow very large quickly and you'll overflow the output storage. The BigInteger is an arbitrarily large data type that accommodates large numbers.

This post has been edited by blackcompe: 20 March 2012 - 01:53 PM

### #3 tlhIn`toq

• Not here as much anymore

Reputation: 6010
• Posts: 13,179
• Joined: 02-June 10

## Re: Missing Return Statement in code to display factorials from 1-100

Posted 20 March 2012 - 01:58 PM

```    while (counter<=100)
{
System.out.print("Factorial of " + counter + " is");
factorial = counter/counter;
for (x=counter; x>0; x--)
{
factorial = factorial*x;

if (counter == 0)
return 1;
else
return counter * factorial( counter-1 ); //recursive call
}
}

```

From the compiler's perspective there is the possibility that counter starts out more than 100. In that case the while loop never executes and you have no return statement in that eventuality.

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: Missing Return Statement in code to display factorials from 1-100

Posted 20 March 2012 - 01:59 PM

A don't think recursion is involved here the only thing the teacher wants to see is that by using int you will overflow which won't be the case with BigInteger
and he wants the result of each factorial to see where it will slip ... around 16 I guess not really need to go to 100

```	public static void main(String[] args) {
int facto = 1;
for(int i = 2; i <= 100; ++i) {
facto *= i;
System.out.printf("Facto of %3d is %12d\n", i, facto);
}
}

```

and with a long you can go further
```	public static void main(String[] args) {
int facto = 1;
for(int i = 2; i <= 100; ++i) {
facto *= i;
System.out.printf("Facto of %3d is %12d\n", i, facto);
}
long lFacto = 1;
for(long i = 2; i <= 100L; ++i) {
lFacto *= i;
System.out.printf("Facto of %3d is %20d\n", i, lFacto);
}
}

```

This post has been edited by pbl: 20 March 2012 - 02:02 PM
Reason for edit:: added the long version

### #5 blackcompe

• D.I.C Lover

Reputation: 1156
• Posts: 2,538
• Joined: 05-May 05

## Re: Missing Return Statement in code to display factorials from 1-100

Posted 20 March 2012 - 02:09 PM

Quote

A don't think recursion is involved here the only thing the teacher wants to see is that by using int you will overflow which won't be the case with BigInteger
and he wants the result of each factorial to see where it will slip ... around 16 I guess not really need to go to 100

Actually I agree completely. Perhaps the submission was rejected because you failed to implement the BigInteger requirement.

### #6 hambonie

Reputation: 0
• Posts: 34
• Joined: 13-March 12

## Re: Missing Return Statement in code to display factorials from 1-100

Posted 21 March 2012 - 06:37 AM

Thanks for the help. i almost have it. but am having trouble getting the BigInteger portion of the code to compile. i don't know how to properly declare j. any ideas?
```//csc 151
//small extra
//factorial from 1-100
//by hambonie

import javax.swing.*;
import java.math.*;

public class FactorialExtra3 {
public static void main(String[] args) {
System.out.println("The factorials from 1-100 are:");
factorial();
}

public static void factorial() {
int facto = 1;
for(int i = 2; i <= 100; ++i) {
facto *= i;
System.out.printf("Facto of %3d is %12d\n", i, facto);
}
System.out.println("BigInteger factorials from 1-100 are:");
BigFactorial();
}
public static void BigFactorial() {
BigInteger factor = new BigInteger("1");
//BigInteger j = new BigInteger("2");
for(BigInteger j = 2; j <= 100; ++j) {
factor *= j;
System.out.printf("Facto of %3d is %12d\n", j, factor);
}

}}

```

### #7 blackcompe

• D.I.C Lover

Reputation: 1156
• Posts: 2,538
• Joined: 05-May 05

## Re: Missing Return Statement in code to display factorials from 1-100

Posted 21 March 2012 - 09:33 AM

1. Don't call BigFactorial from factorial. It's bad design. Call BigFactorial from main right after calling factorial.

2. You can only use binary arithmetic operators with primitive data types and wrapper classes, i.e. int, float, Integer, Byte. BigInteger is an object, which requires calls to its methods to change its internal state. Look at the BigInteger docs to see what methods are available for doing arithmetic.