Skip to content

Commit b46b7ff

Browse files
recursion
1 parent 00ed560 commit b46b7ff

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Notes/C++ Basics[Code]/recursion.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include<iostream>
2+
using namespace std;
3+
int main()
4+
{
5+
int factorial(int);
6+
int fact,value;
7+
cout<<"Enter any number: ";
8+
cin>>value;
9+
fact=factorial(value);
10+
cout<<"Factorial of a number is: "<<fact<<endl;
11+
return 0;
12+
}
13+
int factorial(int n)
14+
{
15+
if(n<0)
16+
return(-1); /*Wrong value*/
17+
if(n==0)
18+
return(1); /*Terminating condition*/
19+
else
20+
{
21+
return(n*factorial(n-1));
22+
}
23+
}

0 commit comments

Comments
 (0)