how to add two numbers in c++ without using any operator?
PeachyAppy
2010-12-03 00:18:30 UTC
how to add two numbers in c++ without using any operator?
Five answers:
?
2010-12-03 06:40:40 UTC
C++ has the plus() functor
#include
#include
int main()
{
int a = 7;
int b = 10;
int c = std::plus()(a, b);
std::cout << a << " + " << b << " = " << c << '\n';
}
although the round parentheses are technically called "function call operator". If that operator is disallowed, then the answer is 'cannot be done', because no function can be called.
If the question was simply to avoid using operator+, then you could do this trivially with operator++:
int a = 7;
int b = 10;
while(b --> 0) ++a;
std::cout << "7 + 10 = " << a << '\n';
MooseBoys
2010-12-03 00:29:24 UTC
This is not possible.
Note that even function calls require use of the () operator, and if you want to store the result, you'd need the = operator.
sirgrantiii
2010-12-03 00:49:36 UTC
I agree with the answer above me. That is like asking how do you do 2 + 2 without the + sign.
Javck
2010-12-03 01:40:11 UTC
In C, using stdio (replace snprintf with _snprintf on Microsoft compilers):
#include
unsigned int JokeAdder(unsigned int a, unsigned int b)
{
return snprintf(NULL, 0, "%*.*s%*.*s", a, a, "", b, b, "");
}
?
2016-12-12 12:01:39 UTC
This software consists of no pluses or minuses in any respect interior this methodology (This consists of ++ and --), and no * or /. It does not comprise %, as an advantage. it somewhat works fairly much like the CPU binary adder circuits : examine each bit one via one, upload them, in case you want to carry then you definitely do so. I make no promises for overflow or adverse integers - seems to artwork yet makes it a lot more suitable complicated. yet this can nonetheless artwork i imagine for effective for any 2 effective numbers whose sum is lower than 2^31. //import java.*? public classification SpecialAdder{ public static void significant(String args[]){ int num1=Integer.parseInt(args[0]);//First integer from command-line int num2=Integer.parseInt(args[a million]);//2d integer from command-line int outcome=0; boolean carry=pretend;//implement the carry as a boolean int mask=a million; for(mask=a million;(mask!=0);mask<<=a million){//bathing room... via each little bit of the integer, if((num1&mask)!=0){//If Num1's present day bit is determined if((num2&mask)!=0){//If Num2's present day bit is determined //We might want to carry over, carry earlier set or no. if(carry){// a million plus a million plus carry outcome|=mask;//Set to at least a million //carry is already real, is left unchanged }else{// a million plus a million //Bit is already 0 carry=real;//We might want to carry } }else{//Else Num2's present day bit isn't set //we may might want to carry over, relying on carry status if(carry){// a million plus 0 plus carry //Bit is already 0 //carry is already real, is left unchanged }else{// a million plus 0 outcome|=mask;//Set to at least a million //carry is already pretend, left unchanged } } }else{//Else Num1's present day bit isn't set if((num2&mask)!=0){//If Num2's present day bit is determined //we may might want to carry over, relying on carry status if(carry){// 0 plus a million plus carry //Bit is already 0 //carry is already real, is left unchanged }else{// 0 plus a million outcome|=mask;//Set to at least a million //carry is already pretend, left unchanged } }else{//Else Num2's present day bit isn't set //we gained't carry over, carry earlier set or no. if(carry){// 0 plus 0 plus carry outcome|=mask;//Set to at least a million carry=pretend;//We give up wearing }else{// 0 plus 0 //Bit is already 0 //carry is already pretend, left unchanged } } } } gadget.out.println("The sum of "+num1+" and "+num2+" is "+outcome); } }
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.