How to resolve the algorithm Factorial step by step in the Apex programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Factorial step by step in the Apex programming language
Table of Contents
Problem Statement
Write a function to return the factorial of a number. Solutions can be iterative or recursive. Support for trapping negative n errors is optional.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Factorial step by step in the Apex programming language
Source code in the apex programming language
public static long fact(final Integer n) {
if (n < 0) {
System.debug('No negative numbers');
return 0;
}
long ans = 1;
for (Integer i = 1; i <= n; i++) {
ans *= i;
}
return ans;
}
public static long factRec(final Integer n) {
if (n < 0){
System.debug('No negative numbers');
return 0;
}
return (n < 2) ? 1 : n * fact(n - 1);
}
You may also check:How to resolve the algorithm JSON step by step in the Arturo programming language
You may also check:How to resolve the algorithm Sparkline in unicode step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Look-and-say sequence step by step in the PowerBASIC programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the XSLT programming language
You may also check:How to resolve the algorithm 100 doors step by step in the M2000 Interpreter programming language