C Programs on Recursion

C Programming Examples - Recursion

Recursion is the process of a function calling itself directly or indirectly, and the associated function is called a recursive function. Recursive functions and algorithms are useful for solving many math problems, tree problems, tower of Hanoi, graph problems, and more. The following section contains various programs on mathematical operations with recursion, strings with recursion, linked lists, and tree algorithms with and without recursion.

Each sample program on the recursion includes a program description, C code, and program output. All examples have been compiled and tested on Windows and Linux systems.

Here is the listing of C programming examples on Recursion & No Recursion:

  1. C Programs on Mathematical Operations using Recursion
  2. C Programs on Linked List using Recursion
  3. C Programs on Linked List without using Recursion
  4. C Programs on Tree using Recursion
  5. C Programs on Tree without using Recursion
  6. C Programs on Strings using Recursion
  7. C Programs on Data Structures using Recursion

1. C Programs on Mathematical Operations using Recursion

Program Description
Sum of Digits using Recursion in C C Program to Find Sum of Digits of a Number using Recursion
Reverse Number using Recursion in C C Program to Reverse a Number using Recursion
Integer Binary Equivalent using Recursion in C C Program to Print Binary Equivalent of an Integer using Recursion
Prime Number using Recursion in C C Program to Check whether a Number is Prime or Not using Recursion
Sum of N Numbers using Recursion in C C Program to Find Sum of Natural Numbers using Recursion
Power using Recursion in C C Program to Calculate the Power using Recursion
Product of two Numbers using Recursion in C C Program to Find Product of Two Numbers using Recursion
Product of two Numbers without Recursion in C C Program to Find Product of Two Numbers without Recursion
Nth Fibonacci Series using Recursion in C C Program to Find Nth Fibonacci Number using Recursion
Factorial using Recursion in C C Program to Find the Factorial of a Number using Recursion
GCD using Recursion in C C Program to Find GCD of Two Numbers using Recursion
LCM using Recursion in C C Program to Find LCM of Two Numbers using Recursion
HCF using Recursion in C C Program to Find HCF using Recursion
HCF without Recursion in C C Program to Find HCF of Two Numbers without Recursion
Binary to Gray Code using Recursion in C C Program to Convert Binary to Gray Code using Recursion
Binary to Gray Code without Recursion in C C Program to Convert Binary to Gray Code without Recursion
Decimal to Binary using Recursion in C C Program to Convert Decimal to Binary using Recursion

advertisement
advertisement

2. C Programs on Linked List using Recursion

Program Description
Search an Element in Linked List using Recursion in C C Program to Search an Element in a Linked List using Recursion
Count Occurrences of Elements in a Linked List using Recursion in C C Program to Count the Occurrences of Elements in a Linked List using Recursion
Length of Linked List using Recursion in C C Program Find the Length of Linked List using Recursion
Print All Nodes of Linked List using Recursion in C C Program to Print All Nodes of Linked List using Recursion
Reverse a Linked List using Recursion in C C Program to Reverse a Linked List using Recursion
Print Alternate Nodes of a Linked List using Recursion in C C Program to Print Alternate Nodes of a Linked List using Recursion

3. C Programs on Linked List without using Recursion

Program Description
Search an Element in Linked List without Recursion in C C Program to Search an Element in Linked List without Recursion
Count Occurrences of Elements in a Linked List without Recursion in C C Program to Count the Occurrences of Elements in a Linked List without Recursion
Length of Linked List without Recursion in C C Program Find the Length of Linked List without Recursion
Print All Nodes of Linked List without Recursion in C C Program to Print All Nodes of Linked List without Recursion
Reverse a Linked List without Recursion in C C Program to Reverse a Linked List without Recursion
Print Alternate Nodes of a Linked List without Recursion in C C Program to Print Alternate Nodes of a Linked List without Recursion

4. C Programs on Tree using Recursion

Program Description
Find Height of Tree using Recursion in C C Program to Find the Height of Tree using Recursion
Search Tree Element using Recursion in C C Program to Search an Element in a Tree Recursively
Tree Traversal using Recursion in C C Program to Traverse the Tree using Recursion
Level Order Traversal using Recursion in C C Program for Level Order Traversal of a Tree using Recursion
Spiral Order Traversal using Recursion in C C Program for Spiral Order Traversal of a Tree using Recursion
DFS Tree Traversal using Recursion in C C Program for Depth First Binary Tree Search using Recursion
Inorder Traversal using Recursion in C C Program to Perform Inorder Traversal of a Binary Tree using Recursion
Preorder Traversal using Recursion in C C Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Postorder Traversal using Recursion in C C Program to Perform Postorder Traversal of a Binary Tree using Recursion

5. C Programs on Tree without using Recursion

Program Description
Tree Traversal without Recursion in C C Program to Traverse the Tree without Recursion
DFS Tree Traversal without Recursion in C C Program for Depth First Binary Tree Search without using Recursion
Inorder Traversal without Recursion in C C Program to Perform Inorder Traversal of a Binary Tree without using Recursion
Preorder Traversal without Recursion in C C Program to Perform Preorder Traversal of a Binary Tree without using Recursion
Postorder Traversal without Recursion in C C Program to Perform Postorder Traversal of a Binary Tree without using Recursion

6. C Programs on Strings using Recursion

Program Description
Reverse a String using Recursion in C C Program to Reverse a String using Recursion
Reverse a String using Recursion and Iteration in C C Program to Reverse a String using Recursion and Iteration
First Uppercase Letter in a String using Recursion in C C Program to Find the First Capital Letter in a String using Recursion
First Uppercase Letter in a String without Recursion in C C Program to Find the First Capital Letter in a String without Recursion
String Length using Recursion in C C Program to Find the Length of the String
String Copy using Recursion in C C Program to Copy One String to Another using Recursion
Palindrome String using Recursion in C C Program to Check whether a String is Palindrome or not using Recursion

7. C Programs on Data Structures using Recursion

Program Description
Largest Element in an Array using Recursion in C C Program to Find Largest Element in an Array using Recursion
Search an Element in an Array using Recursion in C C Program to Search an Element in an Array using Recursion
Matrix Multiplication using Recursion in C C Program to Perform Matrix Multiplication using Recursion
Reverse a Stack using Recursion in C C Program to Reverse a Stack using Recursion
Reverse a Stack without Recursion in C C Program to Reverse a Stack without Recursion
Tower of Hanoi using Recursion in C C Program to Solve Tower of Hanoi using Recursion
Linear Search using Recursion in C C Program to Implement Linear Search using Recursion
Binary Search using Recursion in C C Program to Implement Binary Search using Recursion
Merge Sort using Recursion in C C Program to Perform Merge Sort using Recursion
Quick Sort using Recursion in C C Program to Implement Quick Sort using Recursion
Shell Sort without Recursion in C C Program to Perform Shell Sort without using Recursion
Magic Squares Puzzle without Recursion in C C Program to Solve the Magic Squares Puzzle without Recursion

Sample Recursion Program using C

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

advertisement
/*  
 * C program to find the reverse of a number using recursion
 */
#include <stdio.h>
#include <math.h>
 
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));
    }
}

advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.