Add

Concept of Recursion in 12 usefull Program in C language

Let's see the Concept of Recursion in 12 usefull Program in C language .

Best Way To Learn C Programming free

Basic For Loop program in c language

Top 10 Basic C Programs For Beginners

  

1.Program for Adding Two Numbers Using Recursion.

#include<stdio.h>

int y;

/*

    Function to add two numbers and

    return the result

*/

int add(int m, int n)

{

    if(n == 0)

        return m;

    /*

        Recursion: adding 1, n times and

        then at the end adding m to it

    */

    y = add(m, n-1) + 1;

    return y;   // return the result

}

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int a, b, r;

    printf("Enter the two numbers:\n");

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

    r = add(a, b);     // function call

    printf("\n\nSum of two numbers is: %d\n\n", r);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

2.Program to find Factorial of a Number using Recursion.

#include<stdio.h>

// declaring the function

int fact(int);

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    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);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

int fact(int aj)

{

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

        return 1;

    else

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

}

3.Program to print Fibonacci Series using Recursion.

A Fibonacci series is defined as a series in which each number is the sum of the previous two numbers with 1, 1 being the first two elements of the series.

#include<stdio.h>

// declaring the function

void printFibo(int );

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int k, n;

    long int i = 0, j = 1;

    printf("Enter the length of the Fibonacci series: ");

    scanf("%d", &n);

    printf("\n\nfirst %d terms of Fibonacci series are:\n\n\n",n);

    printf("%d ", 1);

    printFibo(n);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

void printFibo(int aj)

{

    static long int first = 0, second = 1, sum;

    if(aj > 1)

    {

        sum = first + second;

        first = second;

        second = sum;

        printf("%ld ", sum);

        printFibo(aj-1);    // recursive call

    }

    else

    {

        // after the elements, for line break

        printf("\n\n\n");

    }

}

4.Program to find Sum of First N Numbers.

 Program to find sum of the first n numbers using recursion, where the value of n is provided by the user.

#include<stdio.h>

// declaring the recursive function

int getSum(int);

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int n, sum;

    printf("\n\nEnter the range of n: ");

    scanf("%d", &n);

    sum = getSum(n);

    printf("\n\nThe sum of first %d numbers is %d\n", n, sum);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

// function definition

int getSum(int aj)

{

    /*

        static variables hold their values

        till the end of the program

    */

    static int sum = 0;

    if(aj > 0)

    {

        sum = sum + aj;

        getSum(aj-1);

    }

    return sum ;

}

5.Program to find Sum of digits of a Number using Recursion.

  • Program to find sum of digits of a given number using recursion.

#include<stdio.h>

#include<conio.h>

//declaring the recursive function

int sumOfDigit(int num);

void main()

{

    int num, sum;

    clrscr();

    printf("Enter a number:\t");

    scanf("%d", &num);

    sum = sumOfDigit(num);

    printf("The sum of digits of %d is: %d", num, sum);

    getch();

}

int sumOfDigit(int num)

{

    int s, a;

    s = s + (num%10);

    a = num/10;

    if(a > 0)

    {

        sumOfDigit(a);

    }

    return s;

}

6.Program to find Palindrome using Recursion.

  •  Program to find whether the user input number is a palindrome or not using recursion:

#include<stdio.h>

// declaring the recursive function

int isPal(int );

/*

    global declaration to use the same value

    in both the functions

*/

int n;

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    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);

    printf("\n\n\t\t\tCoding is Fun !\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;

}

7.Program to calculate a Number raised to the Power of N using Recursion.

  • Program to calculate the result of a given number, raised to the power of n using recursion.

#include<stdio.h>

// function prototype declaration

int power(int n1, int n2);

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    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));

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

int power(int b, int e)

{

    if(e == 0)

        return 1;

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

}

8.Program to find the largest Element in an Array using Recursion.

#include<stdio.h>

#define MAX 100

int getMaxElement(int []);  // takes array of int as parameter

int size;

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int arr[MAX], max, i;

    printf("\n\nEnter the size of the array: ");

    scanf("%d", &size);

    printf("\n\nEnter %d elements\n\n", size);

    for(i = 0; i < size; i++)

    {

        scanf("%d", &arr[i]);

    }

        max = getMaxElement(arr);   // passing the complete array as parameter

    printf("\n\nLargest element of the array is %d\n\n", max);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

int getMaxElement(int a[])

{

    static int i = 0, max =- 9999;  // static int max=a[0] is invalid

    if(i < size)   // till the last element

    {

        if(max < a[i])

        max = a[i];

       i++;    // to check the next element in the next iteration

        getMaxElement(a);   // recursive call

    }

    return max;

}

9.Program to find whether 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.

#include<stdio.h>

// declaring the recursive function

int isPrime(int, int);

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    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);

    }

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    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

    }

}

10.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.

#include<stdio.h>

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

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    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);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    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;

    }

}

11. Program to find GCD of two Numbers using Recursion.

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

#include<stdio.h>

// declaring the recursive function

int find_gcd(int , int );

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    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);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    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;

}

  • 12.Program to Reverse a String Using Recursion.

#include<stdio.h>

#include<conio.h>

// declaring recursive function

char* reverse(char* str);

void main()

{

    int i, j, k;

    char str[100];

    char *rev;

    printf("Enter the string:\t");

    scanf("%s", str);

    printf("The original string is: %s\n", str);

    rev = reverse(str);

    printf("The reversed string is: %s\n", rev);

    getch();

}

// defining the function

char* reverse(char *str)

{

    static int i = 0;

    static char rev[100];

    if(*str)

    {

        reverse(str+1);

        rev[i++] = *str;

    }

    return rev;

}

 

Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.