What is Explicit?
- Explicit refers to the function that directly defines the solution without relying on the self-calls or recursion.
- The Explicit functions often involve direct calculations iterative loops or explicit formulae.
Example: Calculating the factorial of the number using the explicit loop:
#include <iostream>
// Function to calculate factorial
long long factorial(int n)
{
// Initialize result to 1
long long result = 1;
// Iterate from 1 to n
for (int i = 1; i <= n; i++) {
// Multiply result with the current number
result *= i;
}
// Return the result
return result;
}
// Main method
int main()
{
int num = 5;
long long fact = factorial(num);
std::cout << "The factorial of " << num << " is "
<< fact << std::endl;
return 0;
}
public class Main {
// Function to calculate factorial
public static long factorial(int n)
{
// Initialize result to 1
long result = 1;
// Iterate from 1 to n
for (int i = 1; i <= n; i++) {
// Multiply result with the current number
result *= i;
}
// Return the result
return result;
}
// Main method
public static void main(String[] args)
{
int num = 5;
long fact = factorial(num);
System.out.println("The factorial of " + num
+ " is " + fact);
}
}
def factorial(n):
# Initialize result to 1
result = 1
# Iterate from 1 to n
for i in range(1, n + 1):
# Multiply result with the current number
result *= i
# Return the result
return result
def main():
num = 5
fact = factorial(num)
print("The factorial of", num, "is", fact)
if __name__ == "__main__":
main()
// Function to calculate factorial
function factorial(n) {
// Initialize result to 1
let result = 1;
// Iterate from 1 to n
for (let i = 1; i <= n; i++) {
// Multiply result with the current number
result *= i;
}
// Return the result
return result;
}
// Main method
function main() {
let num = 5;
let fact = factorial(num);
console.log("The factorial of " + num + " is " + fact);
}
// Call the main method
main();
Output
The factorial of 5 is 120
Difference Between Recursive and Explicit
In programming, recursive and explicit are two different approaches used to define functions or algorithms. The Recursive refers to the function that calls itself to solve smaller instances of same problem in while explicit refers to the function that directly defines the solution without relying on the self-calls. This article explores the differences between the recursive and explicit approaches their characteristics and when to use each.