Friday 14 February 2014

Test your Programming Skill : P-1

Write a Program to calculate Sum of Two number Without Using Any operator like +,-,* ... etc.
test your programming skill

You can solve it in many different ways : But you need to consider binary operation like XOR , AND .


Imagine if you have to add two number in Binary what will you do . simply you going to create full adder .
similarly here you have to implement full adder . But not only for 3 bit make it  general . i.e it can add any number .
To do this you can use recursion or looping .

Here we provide you C & Python 3 program sample .

C code Without Recursion :


 #include<stdio.h>  
 int Add(int x, int y)  
 {  
   while (y != 0)   
   // Iterate till there is no carry   
   {  
     int carry = x & y;   
     // carry now contains common set bits of x and y  
     x = x ^ y;   
     // Sum of bits of x and y where at least one of the bits is not set  
     y = carry << 1;  
     // Carry is shifted by one so that adding it to x gives the required sum  
   }  
   return x;  
 }  
 int main()  
 {  
      int a,b;  
      printf("input a & b : ");  
      scanf("%d %d",&a,&b);  
   printf("%d", Add(a, b));  
   return 0;  
 }  

C code With Recursion :


 #include<stdio.h>  
 int Add(int x, int y)  //Recursive problem  
 {  
   if (y == 0)  
     return x;  
   else  
     return Add( x ^ y, (x & y) << 1);  
 }  
 int main()  
 {  
      int a,b;  
      printf("input a & b : ");  
      scanf("%d %d",&a,&b);  
   printf("%d", Add(a, b));  
   return 0;  
 }  

# Python 3 code without recursion :


def ADD(x,y):  
   while(y!=0):  
     carry=x&y  
     x=x^y  
     y=carry<<1  
   return(x)  
x=int(input("insert first no : "))  
y=int(input("insert second no : "))  
print(ADD(x,y))  

# Python 3 code with recursion :


def ADD(x,y):   
   if(y==0):  
     return(x)  
   else:  
     return(ADD(x^y,(x&y)<<1))  
          
   
x=int(input("insert first no : "))   
y=int(input("insert second no : "))   
print(ADD(x,y))  
   
Hope It will Help you :::)

Test your Programming Skill : P-2


Have a Nice Day 8-)

6 comments:

  1. Hello there! I could have sworn I've visited this website before
    but after looking at some of the articles I realized
    it's new to me. Nonetheless, I'm certainly happy I came across it and
    I'll be book-marking it and checking back often!

    Here is my weblog; Christian Louboutin Sydney

    ReplyDelete
  2. I’m not that much of a online reader to
    be honest but your blogs really nice, keep it up! I'll go ahead and bookmark your site to come back later on. All
    the best

    Also visit my page: Louis Vuitton Online

    ReplyDelete
  3. Hey! I'm at work browsing your blog from my new iphone! Just wanted
    to say I love reading through your blog and look forward to all your posts!
    Carry on the excellent work!

    Check out my site; Louboutin Shoes

    ReplyDelete
  4. It's wonderful that you are getting thoughts from this piece of writing as well as from our discussion made at this place.


    Review my homepage :: Christian Louboutin Boots

    ReplyDelete
  5. Hey great blog! Does running a blog like this take a lot of work?
    I've absolutely no expertise in coding however I was hoping to start my own blog soon. Anyways,
    should you have any suggestions or tips for new blog owners please share.
    I know this is off subject however I just had to ask. Kudos!



    My web blog - Discount Christian Louboutin

    ReplyDelete
  6. I’m not that much of a internet reader to be honest but your sites really nice,
    keep it up! I'll go ahead and bookmark your website to come back
    later. All the best

    Here is my blog - Christian Louboutin Heels

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets