[Legacy Content] This page contains very old information, which might not be valid anymore, they are only kept here for legacy purposes.
if you have any inquiries, feel free to contact me! Click here

Recursion - Examples & Solutions

Created: Tuesday, 19 November 2013 Written by Ehab Eldeeb

Write the recursive functions listed below and test them in a program

 

1- Write and test a recursive C-function that returns the factorial of a given integer.

2- Write and test a C-function that returns the value of x raised to the power n recursively.

3- Write and test a recursive C-function that reverse a given string in place.

4- Write and test a recursive C-function that prints out a given string in reverse order.

5- Write and test a C-function that implements a gcd function that returns the greatest common divisor of two given integers. Have your test program invoke both the iterative and the recursive implementations so you can check your results.

6- Write and test the following recursive function that returns the nth square number:
long sqrFun(int n)
the square numbers are 0, 1, 4, 9, 16, 25, 36, …
Note that s(n)=s(n-1)+2n-1 for n>1.

7- Write and test a C-function that implements the Fibonacci series problem recursively.
The Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .
begins with 0, and 1, and each subsequent number is the sum of the preceding two numbers in the series.

8- Write and test a recursive C-function that calculates the series: 1+2+3+4+5+………+N.

9- Write a sequential search function that search for an element in a float array recursively.

 

 

Solution

#include <stdio.h>
#include <conio.h>
#include <string.h>

/* Functions Written by Ehab Eldeeb */

long factorial (int n){
    if (n == 0)
        return 0;
    else if (n == 1)
        return 1;
    else
        return (n * factorial(n-1));
}

float power (float x, int y){
    if (y == 0)
        return 1;
    else
        return (x * power(x,y-1));
}

void rotateString(char mystring[]){
    // will write it later
}

void reversePrint(char mystring[]){
    for(int i = strlen(mystring); i >= 0; i--)
        printf("%c", mystring[i]);
}

int GCD(int x, int y){ // Normal Function
    int m;
    if(x>y)
        m=y;
    else
        m=x;
    for(int i=m;i>=1;i--)
        if(x%i==0 && y%i==0)
            return i;
}

int GCD2(int x, int y){ // Recursive Function
    int m,n;
    if(x > y){
        m = y;
        n = x;
    } else {
        m = x;
        n = y;
    }
    if (n%m == 0)
        return m;
    else
        return GCD2(n,m-1);
}

long sqrFun(int n){
    if (n == 0)
        return 0;
    else if (n == 1)
        return 1;
    else
        return (sqrFun(n-1) + ((2 * n) - 1));
}

int fib(int n) { // 0,1,1,2,3,5,8,13......
    if(n == 0)
        return 0;
    else if (n == 1)
        return 1;
    else
        return (fib(n-1) + fib(n-2));
}

int seriesX (int n){ // 1 + 2 + 3 + 4 + 5 + .... + N
    if (n == 1)
        return 1;
    else
        return (n + seriesX(n-1));
}

void sequentialSearch(){
    // You don't have to do this
}