I have a little problem when calculating the factorial of a number. We are with all the subject of functions, methods, etc. And if it makes me a little difficult, I have to calculate the factorial of a number and my code is as follows:
package factoriales;
import java.util.*;
public class factorialNumero {
public static void main(String[] args) {
System.out.print(Factorial(25));
}
public static String Factorial (int n) {
Scanner teclado = new Scanner(System.in);
System.out.println("Introduzca el numero del que desea ver el factorial");
n = teclado.nextInt();
int producto;
if (n==0) {
return "No hay factorial";
}
if (n==0 || n==1) {
return "1";
}
for (int i = 2; i < n; i++) {
producto = i * n;
}
return null;
}
}
The problem is that the program does not return anything because I have the "return null" but if I put anything else I get a lot of errors. I hope the error is easy to correct.
Greetings, George.
ISSUE
Apparently you are calculating the factorial of a number iteratively.
With these issues cleared up, we can implement a solution to your problem by reviewing what a factorial number means and then applying the appropriate logic so that our program calculates factorial numbers at least even
12!
using only variables of typeint
.SOLUTION
Basically what we want is to create an iterative structure that allows us to multiply a value several times, storing the result in each iteration to multiply it in the next iteration.
We can achieve this using a loop
for
, and as limit values we will use 2 andn
, wheren
is the value of the factorial that we want to calculate. For example, if we want to calculate the factorial of 7, we write the loop like this:Now that we know what we are going to iterate, we are going to start the factorial at 1, since if it
n
takes the values 0 or 1, the factorial of both is 1.Finally, the factorial of a number is another number, so your function must return a numeric value, not a string, as you currently have it.
By definition, the value of a factorial or the factorial function is defined on positive values, so it is something to take into account when creating the program.
To calculate the factorial iteratively, we take an initial value:
1
, and we multiply by each integer that follows the initial value until we reachn
. For example:In a loop
for
, the iteratori
will take the values from2
ton
and we can calculate the factorial by accumulating in each iteration the previous value multiplied by the iterator:In Java (as in other languages) we can use the direct assignment operator of the operation:
*=
, so the previous line can look like this:Your factorial function might look like this:
This function iterates from 2 to
n
. In the event that itn
takes a value less than 2, the loop is simply not entered and the value of the variablefactorial
that is initialized to 1 is returned.As you can see, I have removed from the function the request for the number to the user, since the function must receive this value as an argument.
The complete code of your program might look like this:
I hope this helps you better understand the problem and limitations when calculating a factorial value with type
int
or type representationslong
.