How to Find Factorial in Salesforce Apex Programming.
- Abhilash Banpurkar
- Jan 22, 2023
- 1 min read
Here is an example of how to find the factorial of a number using a for loop in Apex:
Programs:
public class Factorial {
public static Integer factorial(Integer n) {
Integer result = 1;
for (Integer i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}
You can then call the factorial method and pass in the number you want to

find the factorial of, like so:
Integerx=7;
IntegerxFactorial= Factorial.factorial(x);
System.debug(xFactorial);
// Output: 5040
With Real Time Execution Part:

This code creates a class 'Factorial' and its function 'factorial' takes a parameter n

which is the number whose factorial is to be found. A variable 'result' is initialized to 1 and then using a for loop from 1 to n, the result is multiplied by the current value of loop variable i. This continues till the loop ends and the final result is returned.
Comments