Example : A Recursive Function Examples in C to sum the natural numbers the keyboard reads through users.
#include <stdio.h>
#include <conio.h>

int sum(int n);

int main()
{
    int num1, result;

    printf("Enter a positive integer value: ");
    scanf("%d", &num1);

    result = sum(num1);

    printf("The sum is = %d", result);
    return 0;
}

int sum(int num2)
{
    if (num2!=0)
        return num2 + sum(num2-1); 
    else
        return num2;
}

Output :
Enter a positive integer value: 5
The sum is = 15 (hints : 0+1+2+3+4+5)
Example : A Recursive Function Examples in C to find out the factorial result of the given number.
#include <stdio.h>
#include <conio.h>

long int fact(int n);

int main()
{
    int num;
    
    printf("Enter a positive integer for factorial result : ");
    scanf("%d", &num);
    printf("Factorial result of %d is = %ld", num, fact(num));
    
    return 0;
}

long int fact(int num)
{
    if (num>=1)
        return num * fact(num-1);
    else
        return 1;
}
Example : A Recursive Function Examples in C to find out the GCD/HCF result of the given two numbers using recursion.
#include <stdio.h>
#include <conio.h>

int gcd(int num1, int num2);

int main()
{
   int num1, num2;
   printf("Enter two positive integer values: ");
   scanf("%d %d", &num1, &num2);

   printf("G.C.D of %d and %d is = %d .", num1, num2, gcd(num1,num2));
   return 0;
}

int gcd(int num3, int num4)
{
    if (num4 != 0)
       return gcd(num4, num3%num4);
    else 
       return num3;
}
Example : A Recursive Function Examples in C to find out the power of a given number using recursion.
#include <stdio.h>
#include <conio.h>

int power(int num1, int num2);

void main()
{
    int value, exp, result;

    printf("Enter a value : ");
    scanf("%d",&value);

    printf("Enter exponent/power value: ");
    scanf("%d",&exp);

    result = power(value, exp);

    printf("\n\nThe power result of %d^%d is = %d", value, exp, result);
    
    getch();
}

int power(int value1, int exp1)
{
    if (exp1 != 0)
        return (value1 * power(value1, exp1-1));
    else
        return 1;
}
Example : A Recursive Function Examples in C to generate Fibonacci series up to n-terms using recursion.
#include <stdio.h>
#include <conio.h>

void fabo(int a,int b, int num)
{   
    int c;
    if(num>0)
    {
        c=a+b;
        printf("%d ",c);
        a=b;
        b=c;
        
        fabo(a,b,num-1);
    }
}

void main()
{
    int x,y,z,value;
    int i;
     
    x=0;        
    y=1;        
     
    printf("Enter the number of terms for fabonacci series: ");
    scanf("%d",&value);
     
    printf("\n\nFibonacii series are : ");
    
    printf("%d %d ",x,y);
    
    fabo(x,y,value-2);
    
    printf("\n");   
    getch();
}
Example : A C program to count the number of digits present in a given number using recursion.
#include <stdio.h>
#include <conio.h>

int digits(int num2)
{
    static int count=0;
     
    if(num2>0)
    {
        count++;
        digits(num2/10);
    }
    else
    {
        return count;
    }
}

void main()
{
    int num1;
    int total=0;
     
    printf("Enter a positive integer value: ");
    scanf("%d",&num1);
     
    total=digits(num1);
     
    printf("Total digits in the given number %d is: %d\n",num1,total);
     
    return 0;
}
Example : A C program to sum all the digits of a given number using recursion.
#include <stdio.h>
#include <conio.h>

int sumtotal(int num2)
{
    static int sum2=0;
    if(num2>0)
    {
        sum2=sum2+(num2%10); 
        sumtotal(num2/10);
    }
    else
    {
        return sum2;
    }
}
void main()
{
    int num1,sum1;
     
    printf("Enter a positive integer value : ");
    scanf("%d",&num1);
     
    sum1=sumtotal(num1);
     
    printf("Sum of all digits of given value is : %d\n",sum1);
 
    getch();
}
Example : A C program to count the number of character present in given string using recursion.
#include <stdio.h>
#include <conio.h>

int strcount(char *str2)
{
    static int length2=0;
    if(*str2!=NULL)
    {
        length2++;
        strcount(++str2);
    }
    else
    {
        return length2;
    }
}
void main()
{
    char str1[100];
    int length1=0;
     
    printf("Enter a string value: ");
    gets(str1);
     
    length1=strcount(str1);
     
    printf("The length of given strings are : %d\n",length1);
 
    getch();
}

Link for more support in Recursion

Loading

Categories: C

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.