Showing posts with label recursion. Show all posts
Showing posts with label recursion. Show all posts

Monday, September 12, 2011

sum of digits of given number using Recursion

//sum of digits of given number using Recursion
#include<stdio.h>
#include<conio.h>
main()
{
    int a,n;
    clrscr();
    printf("\n enter any number:");
    scanf("%d",&n);
    a=sumdig(n);
    printf("\n sum of digits of the number =%d",a);
    getch();
    }

    sumdig(int num)
    {
    static int sum;
    int a,b;
    a=num%10;
    b=(num-a)/10;
    sum=sum+a;
    if(b!=0)
        sumdig(b);
    else
        return(sum);
    }

sum of two numbers using Recursion

//sum of two numbers using Recursion
#include<stdio.h>
#include<conio.h>
main()
{
    int a,b,i;
    clrscr();
    printf("\nEnter two non-negative nos.:");
    scanf("%d %d",&a,&b);
    printf("\n Sum of the given nos=%d",sum(a,b));
    getch();
}

    sum(int c,int d)
    {
      if(c==0)
        return(d);
      else
        sum(--c,++d);
    }


Fibonacci series using Recursion

Fibonacci series using Recursion

#include<stdio.h>
#include<conio.h>
main()
{
    int n,i;
    clrscr();
    printf("\n How many terms do you want to print:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    printf("\n %d",fib(i));
    }
    getch();
    }

    fib(int m)
    {
       if(m==0)
          return 0;
       if(m==1)
          return 1;
       return (fib(m-1)+ fib(m-2));
    }

exponential series using recursion

#include<stdio.h>
#include<conio.h>
#define ACCURACY 0.00001
main()
{
    int count,n;
    float x,term,sum;
    clrscr();
    printf("\n enter the value of x : ");
    scanf("%f",&x);
    n=term=sum=count=1;
    while(n<100)
      {
         term=term*x/n;
         sum=sum+term;
         count=count+1;
      if(term<ACCURACY)
           n=200;  // to get out of the loop if accuracy b/w terms is less than 0.00001
      else
           n=n+1;
      }
      printf("\n NO. of Terms=%d   sum=%f",count,sum);
           getch();
 }

Decimal to binary using recursion

#include<stdio.h>
#include<conio.h>

main()
{
int n;
clrscr();
printf("\nEnter any decimal no.:");
scanf("%i",&n);
printf("\nBinary equivalent of %d =");
convert(n);
getch();
}

int convert(int x)
{
int i;
    if (x==0)
       return(0);

    i=x%2;
    convert(x/2);
    printf(" %d",i);
}

read about Decimal to Binary

find Binomial coefficient using recursion

Recursion is the process of repeating items in a self-similar way.

#include<stdio.h>
#include<conio.h>
main()
{
    int n,m,x,bino;
    clrscr();
    printf("\n enter the value of n: ");
    scanf("%d",&n);
    printf("m/x");
    for(m=0;m<=n;m++)
       printf("%5d",m);  // print in horz.
    printf("\n---------------------------------------------------------\n");
    m=0;
    do{
        printf("%2d",m); // print for vertical
        x=0;
        bino=1;
           while(x<=m)
           {
           if((m==0)||(x==0))
               printf("%5d",bino);
           else
              {
             bino=bino*(m-x+1)/x;    // calculate C(m,r)
             printf("%5d",bino);
              }
              x=x+1;
        }
        printf("\n");
        m=m+1;
        }while(m<=n);
        getch();
}

Popular 5 Posts Last 7 days