+1 vote
83 views
Is it possible to add two numbers without plus operator?
asked Oct 19, 2015 in Computer Science - IT by T.PRATHIBA | 83 views
which programing language ? you can very well use predefined methods like sum() etc
Programming language like c or java. Give an example as how to use sum()? and in which language it is used?

2 Answers

+1 vote

In C programming ,you can use a tricky method as below :

#include <stdio.h>

int add(int x, int y)
{
    return printf("%*c%*c",x,' ', y,' ');
}

int main()
{
    printf("%d",add(4,5));
    return 0;
}

The above answer will print 4 spaces followed by 5 spaces,so total we have 9 characters(i.e 9 spaces),

When we use %d for 9 characters we have result as 9.

But why go round the head when you can print in single line as sum=x+y ; :)

In predefined method case like in c#.net you can use as below :

  int [] z={1,2};
  MessageBox.Show("" + z.Sum().ToString()); //prints output i message box
 
answered Oct 21, 2015 by slow_learner
Good for c program, actually for java program is there any other method?
@T.PRATHIBA  You have already stated : "Programming language like c or java. Give an example as how to use sum()? and in which language it is used?" ..so this answer must be the solution you can accept know
ok ok, but there is another method, c=a+b can be written as c=a-(-b); that's it without + symbol we have added two numbers and we can use this in any language.
Ha ha..yes you are correct..but why would some one use - - if you can take directly + ,because every character has affect on code metrics know..but its a good puzzle :)..you can add your answer also at enggforum.com so that other students get the correct answer and no question should be without answers...
0 votes
you can try but wise operations

int c= a|b;

it is more faster and optimized when compared to normal addition using +
answered Oct 19, 2015 by kishor93
but pipe symbol is for comparison know !
I think we can use the logical or(|) operator only for binary values, will it suit for normal addition, if it is true just give me a simple program for it kishore.
@T.PRATHIBA but pipe symbol dont give you sum