Recursion COPY Flashcards
public class Main { // Recursive function static void fun(int n) { if (n == 0) return; System.out.print(n + " "); // Print n before recursion fun(n - 1); // Recursive call System.out.print(n + " "); // Print n after recursion } public static void main(String[] args) { fun(3); // Call the function with n = 3 } }
3
2
1
1
2
3
public class RecursionExample {
public static void fun(int n) { if (n == 0) { return; } fun(n - 1); System.out.print(n + " "); // Printing similar to C++'s cout fun(n - 1); } public static void main(String[] args) { fun(3); // Start recursion with 3 } }
1
2
1
3
1
2
1
include using namespace std;
int fun(int n) { if(n == 1) return 0; else return 1 + fun(n / 2); } int main() {
cout<
return 0;
}
It shows the value for log n , 4
include using namespace std;
void fun(int n) { if(n == 0) return;
fun(n / 2);
cout\<} int main() {
fun(7);
return 0;
}
1
1
1
It shows the binary representation of the input number
print numbers from N to 1 using recursion.
include using namespace std;
void printToN(int n) { if(n == 0) return;
cout<
printToN(n - 1);
}
int main() {
int n = 4;
printToN(n);
return 0;
}
print numbers from 1 to N using recursion
include
using namespace std;
void printToN(int n) { if(n == 0) return;
printToN(n - 1);
cout<
}
int main() {
int n = 4;
printToN(n);
return 0;
}
Write a program to calculate factorial with recursion
include stdio.h
int fact(int n) { if(n==0) return 1;
return n*fact(n-1);
}
int main() { printf("Enter the value of N \n"); int n; scanf("%d",&n); printf("%d\n",fact(n));
return 0;
}
Write a program to calculate fibonacci numbers with recursion
include stdio.h
int fib(int n) { if(n\<=1) return n;
return fib(n-1)+fib(n-2); }
int main() { printf("Enter the value of N \n"); int n; scanf("%d",&n); printf("%d\n",fib(n));
return 0;
}
find sum of first n natural numbers.
#include iostream using namespace std;
int getSum(int n) { if(n == 0) return 0;
return n + getSum(n - 1);
}
int main() {
int n = 4;
cout<
return 0;
}
a recursive function to check if a string is palindrome or not
#include iostream using namespace std;
bool isPalindrome(string str, int start, int end) { if(start \>= end) return true;
return ((str[start]==str[end]) &&
isPalindrome(str, start + 1, end - 1));
}
int main() {
string s = “GeekskeeG”;
printf(“%s”, isPalindrome(s, 0, s.length() -1) ? “true” : “false”);
return 0;
}
Sum of Digits Using Recursion
#include iostream using namespace std;
int fun(int n) { if(n \< 10) return n;
return fun(n / 10) + n % 10; }
int main() {
cout
return 0;
}
Rope Cutting Problem:
#include iostream using namespace std;
int maxCuts(int n, int a, int b, int c)
{
if(n == 0)
return 0;
if(n <= -1)
return -1;
int res = max(maxCuts(n-a, a, b, c),
max(maxCuts(n-b, a, b, c),
maxCuts(n-c, a, b, c)));
if(res == -1)
return -1;
return res + 1; } int main() {
int n = 5, a = 2, b = 1, c = 5;
cout maxCuts(n, a, b, c);
return 0;
}
Generate Subsets
#include (iostream\> using namespace std;
void printSub(string str, string curr, int index)
{
if(index == str.length())
{
cout((curr((“ “;
return;
}
printSub(str, curr, index + 1);
printSub(str, curr+str[index], index + 1);
}
int main() {
string str = “ABC”;
printSub(str, “”, 0);
return 0;
}
Output;
C B BC A AC AB ABC
Tower of Hanoi
Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
- Only one disk can be moved at a time.
- Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
- No disk may be placed on top of a smaller disk
// C++ recursive function to // solve tower of hanoi puzzle #include (bits/stdc++.h\> using namespace std;
void towerOfHanoi(int n, char from_rod,
char to_rod, char aux_rod)
{
if (n == 0)
{
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
cout (( “Move disk “ (( n (( “ from rod “ (( from_rod ((
“ to rod “ (( to_rod (( endl;
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
// Driver code int main() { int n = 4; // Number of disks towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods return 0; }
Output
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 3 from rod A to rod B
Move disk 1 from rod C to rod A
Move disk 2 from rod C to rod B
Move disk 1 from rod A to rod B
Move disk 4 from rod A to rod C
Move disk 1 from rod B to rod C
Move disk 2 from rod B to rod A
Move disk 1 from rod C to rod A
Move disk 3 from rod B to rod C
Move disk 1 from rod A to rod B
Move disk 2 from rod A to rod C
Move disk 1 from rod B to rod C
Print 1 to N without Loop
class Solution {
public void printNos(int N)
{
if (N<1)
return;
printNos(N-1);
System.out.print(N+” “);
}
}