You are on page 1of 1

CMP 210 – Data Structures and Algorithms

Spring 2019
LAB-5 Issue Date: March 21, 2019

ALERT!

1. This is an individual lab, you are strictly NOT allowed to discuss your solution with fellow
colleagues, even not allowed asking how is he/she is doing, it may result in negative marking. You
can ONLY discuss with your TAs or with me.
2. Beware of memory leaks and dangling pointers.
3. Pay attention to GOOD coding conventions e.g.
 Proper indentation.
 Meaning variable and function names.
 Use camelCase naming convention
 Use meaningful prompt lines/labels for all input/output
4. Anyone caught in act of coping or cheating would be awarded an “F” grade in this Lab.

Task 01: [50 Marks]


1. Write a recursive function that takes an integer as input and prints every digit of the given number on a separate
line. For example, if the input number is 5481 then output should be:
5
4
8
1

2. Write a program to check whether a number (int) is palindrome or not, using recursion.
Note: A palindromic number is a number that remains the same when its digits are reversed. Like 11, 101, 16461
are palindromes. You are not allowed to use explicit stack or loop in this function.

3. Write a recursive function to check whether a number is prime or not. A prime number is a number that is
divisible only by itself and 1 (e.g. 2, 3, 5, 7, 11).

4. Write a recursive function to print the binary equivalent of a given number. For example, if the given number is 21
then function should print 10101.

5. Write a recursive function that finds whether a substring exist in a given string or not.

6. Write a recursive function that take two integer numbers as input and return their GCD (Greatest Common
Divisor) using Euclidian method.

7. Write a recursive function to count number of characters in a string.

8. Consider the following infinite sequence of positive integers, with the first ten terms shown: 0, 1, 3, 6, 10, 15, 21,
28, 36, 45, . . . Write down a recursive definition of this sequence. And then write a recursive function to find nth
number of the series.

9. Write a recursive function evenDigits that accepts an integer and returns a new number containing only
the even digits, in the same order. If there are no even digits, return 0. Example: evenDigits(8342116)
returns 8426.

10. Write a recursive function void replace(char *str, char from, char to); that changes all occurrences
of from in string str to to. For example, if str were "steve", and from is 'e' and to is 'a', str would
become "stava".

Punjab University College Of Information Technology Page 1 of 1


Madiha Khalid

You might also like