- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteration_Recursion.cpp
More file actions
Latest commit
35 lines (27 loc) · 817 Bytes
/
Copy pathIteration_Recursion.cpp
File metadata and controls
35 lines (27 loc) · 817 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<stdio.h>
#include<conio.h>
intfactorial(int ); /*Recursive Function*/
intfactorial_iter(int );
voidmain()
{
int num;
printf("\nEnter the Number :: ");
scanf_s("%d", &num);
printf("\nThe Factorial of %d through Recursion is :: %d", num, factorial(num));
printf("\nThe Factorial of %d through Iteration is :: %d", num, factorial_iter(num));
_getch();
}
intfactorial(int n) /*Note that a Recursive function has to have an if statement in order to end */
{ /* otherwise it would go into an infinite loop. No Loop is permitted in a recursive function*/
if (n == 0)
return1;
else
return n*factorial(n - 1);
}
intfactorial_iter(int n)
{
int i, fact = 1;
for (i = n; i >= 1; i--)
fact = fact * i;
return fact;
}