You may like to try out some simple problems to practice recursions. Try to solve all of them without using any global variables. And try on your own before looking at the solutions. Also please notify us
Before looking at the problems, you need the idea about how to attack recursive problems.
Here is the beginner idea of recursion.
You will be given an array of integers, write a recursive solution to print it in reverse order.
see answer
Write a recursive function to print an array in the following order.
[0] [n-1]
[1] [n-2]
.........
.........
[(n-1)/2] [n/2]
see answer
Write a recursive program to remove all odd integers from an array. You must not use any extra array or print anything in the function. Just read input, call the recursive function, then print the array in main().
see answer
Write a recursive solution to print the polynomial series for any input n:
1 + x + x2 + ................. + xn-1
see answer
Write a recursive solution to evaluate the previous polynomial for any given x and n.
Like, when x=2 and n=5, we have 1 + x + x2 + ................. + xn-1 = 31
see answer
Write a recursive program to compute n!
see answer
Write a recursive program to compute nth Fibonacci number. 1st and 2nd Fibonacci numbers are 1, 1.
see answer
Write a recursive program to determine whether a given integer is prime or not.
see answer
Write a recursive function that finds the gcd of two non-negative integers.
see answer
Write a recursive solution to compute lcm of two integers. You must not use the formula lcm(a,b) = (a x b) / gcd(a,b); find lcm from scratch...
see answer
Suppose you are given an array of integers in an arbitrary order. Write a recursive solution to find the maximum element from the array.
see answer
Write a recursive solution to find the second maximum number from a given set of integers.
see answer
Implement linear search recursively, i.e. given an array of integers, find a specific value from it.
Input format: first n, the number of elements. Then n integers. Then, q, number of the query, then q integers. Output format: for each of the q integers, print its index (within 0 to n-1) in the array or print 'not found', whichever is appropriate.
see answer
Implement binary search recursively, i.e. given an array of sorted integers, find a query integer from it.
Input format: first n, the number of elements. Then n integers. Then, q, number of the query, then q integers. Output format: for each of the q integers, print its index (within 0 to n-1) in the array or print 'not found', whichever is appropriate.
see answer
Write a recursive solution to get the reverse of a given integer. The function must return an int
see answer
Read a string from keyboard and print it in reversed order. You must not use an array to store the characters. Write recursive solutions to solve this problem.
see answer
Write a recursive program that determines whether a given sentence is palindromic or not just considering the alpha-numeric characters ('a'-Z'), ('A'-'Z'), ('0'-'9').
see answer
Implement strcat(), stracpy(), strcmp() and strlen() recursively.
see answer
If you already solved the problem for finding the nth Fibonacci number, then you must have a clear vision on how the program flow works. So now, in this problem, print the values of your Fibonacci function in the pre-order, in-order and post-order traversal. For example, when n = 5, your program calls 3 and 4 from it, from the call of 3, your program calls 1 and 2 again....... here is the picture:
see answer
All of you have seen the tower of Hanoi. You have 3 pillars 'a', 'b' and 'c', and you need to transfer all disks from one pillar to another. Conditions are, only one disk at a time is movable, and you can never place a larger disk over a smaller one. Write a recursive solution to print the moves that simulate the task, a -> b means move the topmost of the tower a to tower b.
see answer
Before looking at the problems, you need the idea about how to attack recursive problems.
Here is the beginner idea of recursion.
Problem 1:
You will be given an array of integers, write a recursive solution to print it in reverse order.
Input: 5 69 87 45 21 47 Output: 47 21 45 87 69
see answer
Problem 2:
Write a recursive function to print an array in the following order.
[0] [n-1]
[1] [n-2]
.........
.........
[(n-1)/2] [n/2]
Input: 5 1 5 7 8 9 Output: 1 9 5 8 7 7
see answer
Problem 3:
Write a recursive program to remove all odd integers from an array. You must not use any extra array or print anything in the function. Just read input, call the recursive function, then print the array in main().
Input: 6 1 54 88 6 55 7 Output: 54 88 6
see answer
Problem 4:
Write a recursive solution to print the polynomial series for any input n:
1 + x + x2 + ................. + xn-1
Input: 5 Output: 1 + x + x^2 + x^3 + x^4
see answer
Problem 5:
Write a recursive solution to evaluate the previous polynomial for any given x and n.
Like, when x=2 and n=5, we have 1 + x + x2 + ................. + xn-1 = 31
Input: 2 5 Output: 31
see answer
Problem 6:
Write a recursive program to compute n!
Input: 5 Output: 120
see answer
Problem 7:
Write a recursive program to compute nth Fibonacci number. 1st and 2nd Fibonacci numbers are 1, 1.
Input: 6 Output: 8
see answer
Problem 8:
Write a recursive program to determine whether a given integer is prime or not.
Input: 49 999983 1 Output: not prime prime not prime
see answer
Problem 9:
Write a recursive function that finds the gcd of two non-negative integers.
Input: 25 8895 Output: 5
see answer
Problem 10:
Write a recursive solution to compute lcm of two integers. You must not use the formula lcm(a,b) = (a x b) / gcd(a,b); find lcm from scratch...
Input: 23 488 Output: 11224
see answer
Problem 11:
Suppose you are given an array of integers in an arbitrary order. Write a recursive solution to find the maximum element from the array.
Input: 5 7 4 9 6 2 Output: 9
see answer
Problem 12:
Write a recursive solution to find the second maximum number from a given set of integers.
Input: 5 5 8 7 9 3 Output: 8
see answer
Problem 13:
Implement linear search recursively, i.e. given an array of integers, find a specific value from it.
Input format: first n, the number of elements. Then n integers. Then, q, number of the query, then q integers. Output format: for each of the q integers, print its index (within 0 to n-1) in the array or print 'not found', whichever is appropriate.
Input: 5 2 9 4 7 6 2 5 9 Output: not found 1
see answer
Problem 14:
Implement binary search recursively, i.e. given an array of sorted integers, find a query integer from it.
Input format: first n, the number of elements. Then n integers. Then, q, number of the query, then q integers. Output format: for each of the q integers, print its index (within 0 to n-1) in the array or print 'not found', whichever is appropriate.
Input: 5 1 2 3 4 5 2 3 -5 Output: 2 not found
see answer
Problem 15:
Write a recursive solution to get the reverse of a given integer. The function must return an int
Input: 123405 Output: 504321
see answer
Problem 16:
Read a string from keyboard and print it in reversed order. You must not use an array to store the characters. Write recursive solutions to solve this problem.
Input: helloo Output: oolleh
see answer
Problem 17:
Write a recursive program that determines whether a given sentence is palindromic or not just considering the alpha-numeric characters ('a'-Z'), ('A'-'Z'), ('0'-'9').
Input: madam, i'm adam hulala Output: palindromic not palindromic
see answer
Problem 18:
Implement strcat(), stracpy(), strcmp() and strlen() recursively.
Input: test on your own Output: test on your own
see answer
Problem 19:
If you already solved the problem for finding the nth Fibonacci number, then you must have a clear vision on how the program flow works. So now, in this problem, print the values of your Fibonacci function in the pre-order, in-order and post-order traversal. For example, when n = 5, your program calls 3 and 4 from it, from the call of 3, your program calls 1 and 2 again....... here is the picture:
Input: 5 Output: Inorder: 1 3 2 5 2 4 1 3 2 Preorder: 5 3 1 2 4 2 3 1 2 Postorder: 1 2 3 2 1 2 3 4 5
see answer
Problem 20:
All of you have seen the tower of Hanoi. You have 3 pillars 'a', 'b' and 'c', and you need to transfer all disks from one pillar to another. Conditions are, only one disk at a time is movable, and you can never place a larger disk over a smaller one. Write a recursive solution to print the moves that simulate the task, a -> b means move the topmost of the tower a to tower b.
Input: 3 Output: a -> c a -> b c -> b a -> c b -> a b -> c a -> c
see answer
Good Luck!!!
Original Author- Zobayer Vai
No comments:
Post a Comment