DSC-IIITL / GitGrind

8 stars 59 forks source link

Added #31 recursion method to find factorial of any positive number; #47

Closed MrImmortal09 closed 11 months ago

MrImmortal09 commented 11 months ago

31 I have added a new file with method of recursion to find factorial of any number. I have also added long long int, to get answers of bigger numbers. Please review my code @shishiro26 @PavanaSakethaRam

LIT2023002

Illuminati9 commented 11 months ago

Hey @MrImmortal09 , Can you share screenshots and explain your PR very clearly about the changes you have made.

MrImmortal09 commented 11 months ago

Yes definately, First of all program asks for an input through cin. After that a user defined function factorial is called and the input is passed. Then the factorial function starts its execution. It first checks wheather n==0 if yes then returns 1, If no then it returns n*factorial (n-1) where the factorial (n-1) again calls the factorial function itself and the loop goes so on until the if condition state true.

example: Input: 5 Execution;

  1. 5 is passed to factorial function
  2. if(n==0) is false
  3. factorial(4) is called through n x factorial(n-1)
  4. This continues, then at last 1 x factorial (0) is called.
  5. Here the condition of if gets satifised and returns 1
  6. Factorial returns 2 x 1 then 3 x 2 then 4 x 6 then 5 x 24
  7. This finally returns 120 as answer to the first called factorial function in main.

Recursion_Page-1

I have also attached a flow chart explaining the same recursion.