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

Saturday, December 5, 2009

How to add two numbers without using the plus operator?

Actually,
SUM   = A XOR B
CARRY = A AND B
Recursive:
int add(int a, int b){
            if (!a) return b;
            else
                 return add((a & b) << 1, a ^ b);
      }

Iterative

unsigned long add(unsigned long integer1, unsigned long integer2)
{
    unsigned long xor, and, temp;

    and = integer1 & integer2; /* Obtain the carry bits */
    xor = integer1 ^ integer2; /* resulting bits */

    while(and != 0 ) /* stop when carry bits are gone */
    {
        and <<= 1; /* shifting the carry bits one space */
        temp = xor ^ and; /* hold the new xor result bits*/
        and &= xor; /* clear the previous carry bits and assign the new
carry bits */
        xor = temp; /* resulting bits */
    }
    return xor; /* final result */



} 



On a wicked note, you can add two numbers wihtout using the + operator as follows
a - (- b)

Other way is to use ++ operator:
           int a=10,b=20;
          while(b--) a++;
           printf("Sum is :%d",a);  

C program to convert a decimal number into a binary number

#include

void generatebits(int num)
{
  

int temp;
  if(num!=0)
  {
    temp = num % 2;
    generatebits(num >>= 1);
    printf("%d",temp);
  }


}

int main()
{
  int num;
  printf("\nEnter a number\n");
  scanf("%d", &num);
  printf("\n\n");
  generatebits(num);
  return(0);
}

GCD of 2 integers

#include 

int gcd(int a, int b);
int gcd_recurse(int a, int b);



int main()
{
  printf("\nGCD(%2d,%2d) = [%d]", 6,4,  gcd(6,4));



 printf("\nGCD(%2d,%2d) = [%d]", 6,4,  gcd_recurse(6,4));
  return(0);
}

// Iterative algorithm
int gcd(int a, int b)
{
   int temp;
  
   while(b)
   {
      temp = a % b;
      a = b;
      b = temp;
   }

   return(a);
}


// Recursive algorithm
int gcd_recurse(int a, int b)
{
   int temp;

   temp = a % b;

   if (temp == 0)
   {
      return(b);
   }
   else
   {
      return(gcd_recurse(b, temp));
   }
}

Wednesday, December 2, 2009

Reversing a number

int n=12345,m=0;
cout<<"Orginal No:"< 
while(n>0)
{ 
m *= 10; 
m += n%10; 
n /= 10;
} 
cout<<"\nRevesed No:"<
 
 
 
Recursive function 
int reverse_num(int n,int m)

{

if(n==0)

return m; //base (exit condition)

m*=10;

m+=n%10;

return reverse_num(n/10,m); //recursive call

}

Monday, November 30, 2009

Program to generate permutation of characters


Iterative C program


#include 
#define SIZE 3
int main(char *argv[],int argc)
{
  char list[3]={'a','b','c'};
  int i,j,k;

  for(i=0;i
    for(j=0;j
      for(k=0;k
        if(i!=j && j!=k && i!=k)
          printf("%c%c%c\n",list[i],list[j],list[k]);

  return(0);
}



Recursive C program


#include 
#define N  5


int main(char *argv[],int argc)
{
  char list[5]={'a','b','c','d','e'};
  permute(list,0,N);
  return(0);
}


void permute(char list[],int k, int m)
{
  int i;
  char temp;

  if(k==m)
  {
    /* PRINT A FROM k to m! */
    for(i=0;i
    printf("\n");
  }
  else
  {
     for(i=k;i
     {
        /* swap(a[i],a[m-1]); */
        temp=list[i];
        list[i]=list[m-1];
        list[m-1]=temp;

        permute(list,k,m-1);

        /* swap(a[m-1],a[i]); */

        temp=list[m-1];
        list[m-1]=list[i];
        list[i]=temp;
       }
  }
}

Friday, November 27, 2009

pow function

Brute force C program


int pow(int x, int y)
{
  if(y == 1) return x ;
  return x * pow(x, y-1) ;
}



Divide and Conquer C program


#include 
int main(int argc, char*argv[])
{
  printf("\n[%d]\n",pow(5,4));
}

int pow(int x, int n)
{
  if(n==0)return(1);
  else if(n%2==0)
  {
    return(pow(x,n/2)*pow(x,(n/2)));
  }
  else
  {
    return(x*pow(x,n/2)*pow(x,(n/2)));
  }
}