Chapter:

Recursive-program-in-c

1. C Program to find wher a Number is Prime Or Composite using Recursion

Prime Number: A number that is only divisible by 1 and itself.

Composite Number: A number that is not a prime number.

Note: 1 is neither prime nor composite.

Below is a program to find whether the user input number is a prime number or a composite number using recursion.

Source code:

#include


// declaring the recursive function

int isPrime(int, int);


int main()

{

    int num, prime;

    printf("Enter a positive number to check if Prime: ");

    scanf("%d", &num);

    prime = isPrime(num, num/2);

    if(prime == 1)

    {

        printf("\n\n%d is a prime number\n\n", num);

    }

    else

    {

        printf("\n\n%d is a Composite number\n\n", num);

    }

     return 0;

}


// function definition

int isPrime(int n, int i)

{

    if(i == 1)

        return 1;   // return statement terminates the recursive funtion

    else

    {

        if(n%i == 0)

            return 0;

        else

            isPrime(n, i-1);    // recursive call not using return statement

    }

}

Show More

2. C Program to Find Reverse of a Number using Recursion

Problem Description

This C program finds the reverse of a number using recursion.

Problem Solution

The following C program using recursion reverses the digits of the number and displays it on the output of the terminal. Eg: 123 becomes 321.

Program/Source Code

#include

#include

 

int rev(int, int);

 

int main()

{

    int num, result;

    int length = 0, temp;

 

    printf("Enter an integer number to reverse: ");

    scanf("%d", &num);

    temp = num;

    while (temp != 0)

    {

        length++;

        temp = temp / 10;

    }

    result = rev(num, length);

    printf("The reverse of %d is %d.\n", num, result);

    return 0;

}

 

int rev(int num, int len)

{

    if (len == 1)

    {

        return num;

    }

    else

    {

        return (((num % 10) * pow(10, len - 1)) + rev(num / 10, --len));

    }

}

Program Explanation

In this C program, we are reading the integer number using the num variable. Assign the value of num variable to temp variable. While loop is used to check the condition the value of temp variable is not equal to 0, if the condition is true execute the statement divide the value of temp variable by 10.

The result variable is used to call the rev() function by passing num and length variable value as argument. The function rev() is used to reverse the digits of the number. If else condition statement is used to check the value of len variable is equal to 1. If the condition is true execute the statement.

Otherwise, if the condition is false execute the statement. Compute the modulus of the value of num variable by 10 integer and multiply the resulted value with 10. Compute the power of the value of len variable using pow() function. Add the resulted value num variable with 10. Print the reverse of a number using recursion.

Output:

Enter an integer number to reverse: 1234

The reverse of 1234 is 4321.

Show More

3. Program to find Palindrome using Recursion

A Palindrome is a sequence that if reversed looks identical to the original sequence Eg : abba, level, 999 etc.

Below is a simple C program to find whether the user input number is a palindrome or not using recursion:

Source code:

#include


// declaring the recursive function

int isPal(int );


/*

    global declaration to use the same value 

    in both the functions

*/

int n;


int main()

{

    int palindrome;

    printf("\n\nEnter a number to check for Palindrome: ");

    scanf("%d", &n);

    palindrome = isPal(n);

    if(palindrome == 1)

        printf("\n\n\n%d is palindrome\n\n", n);

    else

        printf("\n\n\n%d is not palindrome\n\n", n);

    return 0;

}


int isPal(int aj)

{

    static int sum = 0;

    if(aj != 0)

    {

        sum = sum *10 + aj%10;

        isPal(aj/10);   // recursive call same as while(n!=0) using loop

    }

    else if(sum == n)

        return 1;

    else

        return 0;

}

Show More

4. C Program to Print Binary Equivalent of an Integer using Recursion

Problem Description

This C program, using recursion, finds the binary equivalent of a decimal number entered by the user.

Problem Solution

Decimal numbers are of base 10 while binary numbers are of base 2.

Program/Source Code

#include

 

int binary_conversion(int);

 

int main()

{

   int num, bin;

 

   printf("Enter a decimal number: ");

   scanf("%d", &num);

   bin = binary_conversion(num);

   printf("The binary equivalent of %d is %d\n", num, bin);

}

 

int binary_conversion(int num)

{

    if (num == 0)

    {

        return 0;

    }

    else

    {

        return (num % 2) + 10 * binary_conversion(num / 2);

    }

}

Program Explanation

In this C program, we are reading a decimal number using num variable. Decimal numbers are of base 10, while binary numbers are of base 2. The binary_conversion() function is used to find the binary equivalent of a decimal number entered by the user.

In binary_conversion() function, convert the binary number to its equivalent decimal value. If else condition statement is used to check the value of num variable is equal to 0. If the condition is true, execute the statement by returning 0 to the called variable bin.

Otherwise if the condition is false, execute else statement. Compute the modulus of the value of num variable by 2 and add the resulted value to 10. Multiply the resulted value with the value of binary_conversion() function. Divide the value of num variable by 2 and pass as an argument and execute the function recursively. Print the Binary equivalent of an integer using recursion.

Output:

Enter a decimal number: 10

The binary equivalent of 10 is 1010

Show More

5. C Program to find GCD of two Numbers using Recursion

Greatest Common Divisor(GCD) of two numbers is a number that divides both of them.

Below is a program to the GCD of the two user input numbers using recursion.

Source code:

#include

// declaring the recursive function

int find_gcd(int , int );

int main()

{

    int a, b, gcd;

    printf("\n\nEnter two numbers to find GCD of \n");

    scanf("%d%d", &a, &b);

    gcd = find_gcd(a, b);

    printf("\n\nGCD of %d and %d is: %d\n\n", a, b, gcd);

    return 0;

}

// defining the function

int find_gcd(int x, int y)

{

    if(x > y)

        find_gcd(x-y, y);

    else if(y > x)

        find_gcd(x, y-x);

    else

    return x;

}

Show More

6. Program to find Factorial of a Number using Recursion

Below is a program for finding factorial of a given number using recursion.

Source code:

#include

// declaring the function

int fact(int);


int main()

{

    int num, f;

    printf("\n\nEnter a number: ");

    scanf("%d", &num);

    f= fact(num);

    printf("\n\nFactorial of  %d is %d\n\n", num, f);

     return 0;

}


int fact(int aj)

{

    if(aj==1 || aj==0)

        return 1;

    else

        return (aj*fact(aj-1));

}

Show More

7. C Program to find LCM of two Numbers using Recursion

LCM: Least Common Multiple of two numbers is the number that is a common multiple of the both the numbers.

Below is a program to find LCM of two numbers using recursion.

#include


int find_lcm(int, int);   // function prototype declaration


int main()

{

    int a, b, lcm;

    printf("\n\nEnter 2 integers to find LCM of:\n");

    scanf("%d%d", &a, &b);

    lcm = find_lcm(a,b);    // function call

    printf("\n\n LCM of %d and %d is: %d\n\n", a, b, lcm);

    return 0;

}


int find_lcm(int a, int b)  // function definition

{

    /*

        static variable is initialized only once 

        for each function call

    */

    static int temp = 1;    

    if(temp%a == 0 && temp%b == 0)

    {

        return temp;

    }

    else

    {

        temp++;

        find_lcm(a,b);

        return temp;

    }

}

Show More

8. C Program to Find Sum of Digits of a Number using Recursion

This is a C program to find sum of digits of a number using recursion.

Problem Description

This C program finds the sum of digits of a number using recursion.

Problem Solution

The following C program, using recursion, finds the sum of its digits.

advertisement

Program/Source Code


#include

 

int sum (int a);

 

int main()

{

    int num, result;

 

    printf("Enter the number: ");

    scanf("%d", &num);

    result = sum(num);

    printf("Sum of digits in %d is %d\n", num, result);

    return 0;

}

 

int sum (int num)

{

    if (num != 0)

    {

        return (num % 10 + sum (num / 10));

    }

    else

    {

       return 0;

    }

}

Program Explanation

In this C program, we are reading the integer number using the num variable. The function sum() is used to find sum of digits of a number using recursion.

In function sum() check the value of num variable is not equal to 0. If the condition is true execute the statement. Divide the value of num variable by 10 integer value. Add the resulted value along with the modulus of the value of num variable. Print the sum of digits of a number using recursion.

Output:

Enter the number: 2345

Sum of digits in 2345 is 14

Show More

9. C Program to Find Product of 2 Numbers using Recursion

Problem Description

This C program finds the product of 2 numbers using recursion.

Problem Solution

This C program using recursion, finds the product of 2 numbers without using the multiplication operator.

Program/Source Code

#include

 

int product(int, int);

 

int main()

{

    int a, b, result;

 

    printf("Enter two numbers to find their product: ");

    scanf("%d%d", &a, &b);

    result = product(a, b);

    printf("Product of %d and %d is %d\n", a, b, result);

    return 0;

}

 

int product(int a, int b)

{

    if (a < b)

    {

        return product(b, a);

    }

    else if (b != 0)

    {

        return (a + product(a, b - 1));

    }

    else

    {

        return 0;

    }

}

Program Explanation

In this C program, reading two numbers using a and b variables respectively. The product() function is used to find the product of two numbers. Nested if else condition statement is used to check the value of a variable is less than the value of b variable.

If the condition is true then execute the statement. Compute the summation of the value of a variable with the value. Otherwise, if the condition is false then execute else if condition statement. Check the condition that the value of b variable is not equal to 0.

If the condition is true then execute the statement. Otherwise, if the condition is false then execute the else statement and return the null. Print the product of two numbers.

Output:

 Enter two numbers to find their product: 176 340

Product of 176 and 340 is 59840

Show More

10. C Program to calculate a Number raised to Power of N using Recursion

Below is a program to calculate the result of a given number, raised to the power of n using recursion.

Source code:

#include


// function prototype declaration

int power(int n1, int n2);


int main()

{

    int base, exp;

    printf("Enter base number: ");

    scanf("%d", &base);

    printf("\n\nEnter Power factor: ");

    scanf("%d", &exp);

    printf("\n\n\n\t\t\t%d^%d = %d", base, exp, power(base, exp));

     return 0;

}


int power(int b, int e)

{

    if(e == 0)

        return 1;


    return (b*power(b, e-1));

}

Show More

All Chapters

View all Chapter and number of question available From each chapter from C-programming-