Sunday, July 12, 2009

Can i sum 2 int without using + operators in c language?

You can use the subtraction operator to "add". Or if you really feel like a challenge, traverse through each pair of bits and do bitwise addition. ^_^

Can i sum 2 int without using + operators in c language?
yes using increment or subtraction operator








use increment operator in loop or do double subtraction
Reply:HINT


#include %26lt;stdio.h%26gt;


#include %26lt;conio.h%26gt;





unsigned int add(unsigned int a, unsigned int b)


{


unsigned int c= 0;


unsigned int r= 0;


unsigned int t= ~0;


for (t= ~0; t; t%26gt;%26gt;= 1)


{r%26lt;%26lt;= 1;


r|= (a^b^c)%26amp;1;


c= ((a|b)%26amp;c|a%26amp;b)%26amp;1;


a%26gt;%26gt;= 1;


b%26gt;%26gt;= 1;}


for (t=~0, c=~t; t; t%26gt;%26gt;= 1)


{c%26lt;%26lt;= 1;


c|= r%26amp;1;


r%26gt;%26gt;= 1;}


return c;}





void main ( void ) //so much greater than int main


{


int t;


t = add (7,3);


printf("%d",t);


getch(); //pause program


system("pause"); //just incase getch don't work


}
Reply:I have a different approach for you. You can create a sum function and store it in you header file nextime you can call this function and you ahve need to sue + operator





e:g





void main()


{





int a,b,c;


int sum(int,int); // Function Prototype


a=10;


b=20;





c=sum(a,b)'





getch();


}








/* Store this part in your header file eg: yourheader.h


and next time you just have to include this header file inprogram and you can call sum() function anywhere


c=sum(a,b); */





int sum(int x, int y) // Function definition


{


int z;





z=x+y;


retrun z;


}
Reply:Chk the link








http://in.answers.yahoo.com/question/ind...
Reply:No

narcissus

No comments:

Post a Comment