20_10

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

{

    static long binary,remainder,multiplier = 1;


    if(decimal != 0)

    {

        remainder = decimal % 2;

        binary = binary + remainder * multiplier;

        multiplier = multiplier * 10;

        convertB_to_D(decimal / 2);

    }

    return binary;

}

int main()

{

    long binary;

    int decimal;

printf("Enter a decimal number: ");

    scanf("%d",&decimal); 


    binary = convertB_to_D(decimal);

    printf("The Binary value is : %ld\n", binary);

    return 0;

}

Output:
program in C to convert a decimal number to binary using recursion


Write a program in C to find GCD of two numbers using recursion.

 Source code:

//Write a program in C to find GCD of two numbers using recursion.


#include<stdio.h>

int GCD_cal(int a,int b)

{

    while(a!=b)

    {

        if(a>b)

            return GCD_cal(a-b,b);

        else

            return GCD_cal(a,b-a);

    }

    return a;

}

int main()

{

int num1, num2, gcd;

printf("Enter the first number: ");

scanf("%d",&num1); /*Enter 1st number */

printf("Enter the second number: ");

scanf("%d",&num2); /*Enter 2nd number */


  gcd = GCD_cal(num1,num2);

  printf("The GCD of %d and %d is: %d\n",num1,num2,gcd);

  return 0;

}

Output:

program in C to find GCD of two numbers using recursion


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