Sunday, July 12, 2009

In C++ WAP to add two numbers without + operator?

i was asked this ques by a senior.


answer is very simple letz see if som1 can answer it.


NOTE: dont use any operators


PLS write the whole program

In C++ WAP to add two numbers without + operator?
You say don't use any operators, but that pretty much makes almost every program impossible. I assume that you don't care about some of the "other" operators that include the stream insertion operators for inputting and outputting results, the function call operator, and the member operator, at the very least. List of operators:


http://en.wikipedia.org/wiki/Operators_i...





If you only want to restrict the use of +/-, here's someone that wrote an adder that sort of simulates how it's done in hardware, but he uses lots of bitshift and bitwise operators:


http://www.devmaster.net/forums/showthre...





Here's my little solution, which doesn't even utilize assignment or comparison operators, though it isn't very useful for adding large numbers since it allocates memory then just looks at how much is allocated.








#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;





int main(void)


{


int a, b;


string sum;





cin %26gt;%26gt; a %26gt;%26gt; b;


sum.resize(a, 'z');


sum.append(b, 'z');


cout %26lt;%26lt; sum.size() %26lt;%26lt; endl;


return 0;


}


// it wouldn't let me indent when I pasted...
Reply:try this operator


one+=two;


No comments:

Post a Comment