Advertisement
Iam_Sandeep

Sum of two numbers without using addition operator

Jul 19th, 2022
1,074
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.31 KB | None | 0 0
  1. class Solution {
  2.     public int getSum(int a, int b) {
  3.      while (b!=0){
  4.         int temp= (a&b)<<1;
  5.          a=a^b;
  6.          b=temp;
  7.      }  
  8.         return a;
  9.     }
  10. }
  11. //Recursive
  12. int getSum(int a, int b) {
  13.     return b==0? a:getSum(a^b, (a&b)<<1); //be careful about the terminating condition;
  14. }
  15.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement