20_10

Write a program in C to count the number of digits of a given number using recursion.

 Source code:

//Write a program in C to count the number of digits of a given number using recursion. 


#include<stdio.h>

int noOfDigits(int n)

{

    static int digit=0;


    if(n!=0)

    {

        digit++;

        noOfDigits(n/10);

    }


    return digit;

}

int main()

{

  int n,digit;

    printf("Enter a number: ");

    scanf("%d",&n); /*Enter Number of digits */


    digit = noOfDigits(n);


    printf("The number of digits in the number is :  %d\n",digit);

    return 0;

}

Output:
count the number of digits of a given number using recursion


Write a C program to print the solution of the series f(n)= (1) + (2*3) + (4*5*6) ... upto n terms using recursion.

 Source code:

/* Program to print the solution of the series  f(n)= (1) + (2*3) + (4*5*6) ... upto n terms using recursion.*/


#include <stdio.h>

int seriesSum(int calculated, int current, int N)

{

    int i, cur = 1;

    // checking termination condition

    if (current == N + 1)

        return 0;


    // product of terms till current

    for (i = calculated; i < calculated + current; i++)

        cur *= i;


    // recursive call for adding terms next in the series

    return cur + seriesSum(i, current + 1, N);

}

int main()

{

    int N;

    printf("Enter the nth term: ");

    scanf("%d", &N); /* Enter number to terms N to be calculated */

    /* Write the function int seriesSum(int calculated, int current, int N) which finds the sum of the series

using recursion.  calculated - number of terms till which sum of terms has 

been calculated; current - number of terms for which sum has to be calculated; N - Number of terms in the function to be calculated */

    printf("The result of the series is: %d\n", seriesSum(1, 1, N));

    return 0;

}

Output:
C program to print the solution of the series  f(n)= (1) + (2*3) + (4*5*6) ... upto n terms using recursion


Write a C program to calculate factorial of a number using recursion.

 Source code:

//Write a C program to calculate factorial of a number using recursion.


#include<stdio.h>

int factorial(int n)

{

    if(n==0)

        return 1;

    else

        return (factorial(n-1)*n);

}

int main()

{

    int num,f;

    printf("Enter a number: ");

    scanf("%d",&num);/*Enter the number whose factorial to be found*/

    f=factorial(num); 

/*Write the factorial(num) function which calculates the function in recursive method and returns f */

    printf("Factorial of %d = %d",num,f);

    return 0;

}

Output:
C program to calculate factorial of a number using recursion


Write a C program to insert an element / item in the sorted array.

 Source code:

/*Write a C program to insert an element / item in the sorted array.  */


#include<stdio.h>

int main( )

{

  int a[20],n=10,item,i;

    printf("Enter the sorted array: ");

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

    {

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

    }

    printf("Enter the item which to be inserted in the array: ");

    scanf("%d", &item);

i = n-1;

    while(item<a[i] && i>=0)

    {

           a[i+1] = a[i];

           i--;

    }

    a[i+1] = item;

    n++;

printf("ITEM to be inserted:%d\n",item);

printf("After insertion array is:\n");

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

    {

        printf("%d\n",a[i]);

    }

    return 0;

}

Output:
C program to insert an element or item in the sorted array


Write a C program to find a root of the equation a*x*x + b*x +c using Bi Section method.

 Source code:

/*Numerical Method: Bi Section method to find a root of the equation a*x*x + b*x +c.  The program read should the values of a,b and c and values for starting left and right points. */


#include <stdio.h>

#include <math.h>

double myFunction(double x, double a, double b,double c)

{

  return a*x*x + b*x +c;

}

int main(void){

double a,b,c;

double leftpt, rightpt, midpt, epsilon = .0000001;

double midvalue, rtvalue, root;

printf("Enter the value of 'a','b' and 'c': ");

scanf("%lf %lf %lf", &a, &b, &c);

printf("Enter the value of left and right point: ");

scanf("%lf %lf", &leftpt, &rightpt);

do {

        midpt = (leftpt + rightpt)/2;

        rtvalue = myFunction(rightpt,a,b,c);

        midvalue = myFunction(midpt,a,b,c);

        if (rtvalue * midvalue >= 0)

            rightpt = midpt;

        else 

leftpt  = midpt;

} while ((rightpt - leftpt) > epsilon);


root = (rightpt+leftpt)/2;

printf("Root for equation%5.2lf*x**2+%5.2lf*x+%5.2lf is",a,b,c);

printf("%5.2lf\n",root);

return 0;

}

Output:

C program to find a root of the equation using Bi Section method


Write a C program which will generate Fibonacci sequence using recursive function printFibonacci(int n).

 Source code:

/*A recursive procedure or routine is one that has the ability to call itself. Consider a C program which will generate Fibonacci sequence using function printFibonacci(int n). The sequence is characterized by the fact that every number after the first two is the sum of the two preceding ones. By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two. Here the default is 0,1. So in case of input range 10 it will generate 12 numbers. */


#include<stdio.h>

void printFibonacci(int n){


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


    if(n>0){

        sum = first + second;

        first = second;

        second = sum;

        printf("%d ",sum);

        printFibonacci(n-1);

    }

}

void printFibonacci(int);


int main(){

    int k,n;

    long int i=0,j=1,f;

    printf("Enter the nth term: ");

    scanf("%d",&n);


printf("%d %d ",0,1);

printFibonacci(n);

    return 0;

}

Output:
C program which will generate Fibonacci sequence using recursive function


Write a program in C to convert a decimal number to binary using recursion.

 Source code: //Write a program in C to convert a decimal number to binary using recursion. #include<stdio.h> long convertB_to_D(int d...