Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Factorial of a number using recursion

#include <stdio.h>
int factorial(int number){
    if(number==1){
        return number;
    }
    return number*factorial(number - 1);
}
int main(){
    int a=factorial(5);
    printf("%d",a);
}
Comment

recursion factorial algorithm

FUNCTION FACTORIAL (N: INTEGER): INTEGER
(* RECURSIVE COMPUTATION OF N FACTORIAL *)

BEGIN
  (* TEST FOR STOPPING STATE *)
  IF N <= 0 THEN
    FACTORIAL := 1
  ELSE
    FACTORIAL := N * FACTORIAL(N - 1)
END; (* FACTORIAL *)
Comment

factorial recursive

N= int(input())
def fun(n):
    if n ==1 or n==0:
        return 1
    else:
        n = n * fun(n-1)
        return n 


print(fun(N))
Comment

recursive factorial of a number

factorial(N){
    if(N<=1)
    {
      return 1;
    }
    else
    {
      return (N*factorial(N-1));
    }
}
Comment

factorial recursion

function factorial(n) {
  // Base case
  if (n === 0 || n === 1) return 1;
  // Recursive case
  return n * factorial(n — 1);
}
Comment

factorial using recursion

def fact(n):
  if n==0 or n==1:
    return 1
  else:
    return n*fact(n-1)
print(fact(4))
print(fact(3))
print(fact(0))
Comment

Recursive function of factorial

# Factorial of a number using recursion 
def recur_factorial(n): 
 if n == 1: 
    return n 
 else: 
      return n*recur_factorial(n-1) 
num = 6 
# check if the number is negative 
if num < 0: 
 print("Sorry, factorial does not exist for negative numbers") 
elif num == 0: 
 print("The factorial of 0 is 1") 
else: 
 print("The factorial of", num, "is", recur_factorial(num)) 
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to print a word in c++ 
Cpp :: c++ program to convert fahrenheit to celsius 
Cpp :: cout stack in c++ 
Cpp :: bit++ codeforces in c++ 
Cpp :: c++ string find last number 
Cpp :: c++ linked list delete node 
Cpp :: how to extract a folder using python 
Cpp :: statements 
Cpp :: c++ swap function 
Cpp :: c++ define array with values 
Cpp :: what is the time complexitry of std::sort 
Cpp :: split string in c++ 
Cpp :: Temparory Email Id 
Cpp :: c++ function pointer as variable 
Cpp :: c++ class constructor variable arguments 
Cpp :: converting int to string c++ 
Cpp :: find second largest number in array c++ 
Cpp :: max and min function in c++ 
Cpp :: c++ segmentation fault 
Cpp :: check if cin got the wrong type 
Cpp :: how atan work c++ 
Cpp :: 41.00 
Cpp :: top array data structure questions in inteviews 
Cpp :: c shortest path dijkstra 
Cpp :: cpp how to add collisions to boxes 
Cpp :: print float up to 3 decimal places in c++ 
Cpp :: A Subtask Problem codechef solution in cpp 
Cpp :: codeforces problem 1700A solution in c++ 
Cpp :: hackerearth questions siemens 
Cpp :: foo foo little dogs 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =