Hamza Mohammed - 5 months ago 59

Java Question

Write a program to calculate and print the super factorial of a number using a for loop. The factorial of a number is the product of all integers up to and including that number, so the factorial of 4 (written as 4!) is 4*3*2*1= 24.

The super factorial is the product of all factorials up to and including that factorial.

4!!=4!*3!*2!*1!

I found "factorial" using the following code:

`import java.util.Scanner;`

public class superfactorial {

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner input = new Scanner (System.in);

// number whose factorial to be found

int number;

// prompting input

System.out.print("Enter number: ");

number = input.nextInt();

int factorial = factorial(number);

System.out.printf("the factorial of %d is %d", number , factorial);

}

// method that calculates the factorial

public static int factorial (int n){

int output = 1;

for (int i=1; i <= n; i++) {

output = output * i;

}

return output;

}

}

Answer

The most important line in your factorial method is this line:

```
output = output * i;
```

You multiply `output`

by `i`

where `i`

is an integer that keeps increasing by one.

What's the difference between a superfactorial and a normal factorial? To evaluate a superfactorial, you multiply `output`

not by `i`

, but by the factorial of `i`

, right?

So just do it! I have already explained the whole thing to you! Just create a new method called `superfactorial`

, copy all the stuff from the factorial method and change this line:

```
output = output * i;
```

to this:

```
output = output * factorial(i);
```