diff --git a/Recursion/headRecursion.cpp b/Recursion/headRecursion.cpp new file mode 100644 index 0000000..b51e2db --- /dev/null +++ b/Recursion/headRecursion.cpp @@ -0,0 +1,21 @@ +#include +#include + +using namespace std; + +void fun1(int n){ + +if(n > 0){ + + fun1(n-1); + cout << n <> x; + fun1(x); + return 0; +} diff --git a/Recursion/indirectRecursion.cpp b/Recursion/indirectRecursion.cpp new file mode 100644 index 0000000..576f59a --- /dev/null +++ b/Recursion/indirectRecursion.cpp @@ -0,0 +1,23 @@ +#include +#include + +using namespace std; +void fun2(int); +void fun1(int n){ +if(n > 0){ + cout << n < 1){ + cout << n <> x; + fun1(x); + return 0; +} diff --git a/Recursion/knightproblem.cpp b/Recursion/knightproblem.cpp new file mode 100644 index 0000000..62dce3d --- /dev/null +++ b/Recursion/knightproblem.cpp @@ -0,0 +1,59 @@ +#include + +#include + +using namespace std; + +const int D =8; + +bool canPlace(int board[D][D],int n,int r,int c){ + return + r >= 0 && r =0 && c < n && + board[r][c]==0; +} + bool solveKnight(int board[D][D],int n,int move_no,int curRow,int curCol){ + if(move_no == n*n){ + return true; + } + int row_dir[]= {+2,+1,-1,-2,-2,-1,+1,+2}; + int col_dir[]= {+1,+2,+2,+1,-1,-2,-2,-1}; + for(int cur_dir =0;cur_dir<8;cur_dir++){ + int nextRow = curRow + row_dir[cur_dir]; + int nextCol = curCol + col_dir[cur_dir]; + if(canPlace(board,n,nextRow,nextCol) == true){ + board[nextRow][nextCol]= move_no + 1; + bool isSucess= solveKnight(board,n,move_no+1,nextRow,nextCol); + if(isSucess ==true){ + return true; + }else { + board[nextRow][nextCol]=0; + + } + } + } + return false; + } + void printBoard(int board[D][D],int n){ + for(int i=0;i>n; + + board[0][0]=1; +bool ans = solveKnight(board,n,1,0,0); +if(ans == true ){ + + printBoard(board,n); +}else { + cout << "sorry!!!"; +} +} diff --git a/Recursion/nestedRecursion.cpp b/Recursion/nestedRecursion.cpp new file mode 100644 index 0000000..bc63fae --- /dev/null +++ b/Recursion/nestedRecursion.cpp @@ -0,0 +1,18 @@ +#include +#include + +using namespace std; +int fun1(int n){ +if(n > 100){ + return n-10; +}else{ +return fun1(fun1(n+11)); +}} +int main(){ + int x; + int y; + cin >> x; +y =fun1(x); +cout << y < +#include + +using namespace std; + +void fun1(int n){ + +if(n > 0){ + cout << n <> x; + fun1(x); + return 0; +} diff --git a/Recursion/treeRecursion.cpp b/Recursion/treeRecursion.cpp new file mode 100644 index 0000000..2a3d13c --- /dev/null +++ b/Recursion/treeRecursion.cpp @@ -0,0 +1,17 @@ +#include +#include + +using namespace std; +void fun1(int n){ +if(n > 0){ + cout << n <> x; + fun1(x); + return 0; +}