New course! Every coder should learn Generative AI!
Try a free lesson+ 3
Can any one add two numbers without using + operator?
8 Answers
+ 7
#include<stdio.h>
int main() {
int num1 = 16,
num2 = 14,
i;
//Result should be 30
while (num2 > 0) {
num1++;
num2--;
}
printf("%d", num1);
return (0);
}
In this example, I use the while loop to do it. " #include<stdio.h> " allows me to use "printf" which is the same as using 'cout' to print out text, but I prefer to use printf
By the way, this is for C++, not really for Java
+ 6
#include<stdio.h>
//Insert main here
int y = 4;
int x = 6; //we are going to do y+x
int z = -1;
while (!(x==-6))
{
x--;
}
x = x-y;
int Result = x/z;
printf("%d", Result);
return (0);
//outputs 10. No plus operator used, only minus
+ 1
nope + is used still... I don't wanna see + operator at all
+ 1
int no1,no2;
while(no1--)
no2++;
cout<<no2;
//use it only if no1 is +ve
☺
0
Nope. if you use decrement(++),it also use + operator😂
0
Yes! it's possible via Binary Operators and bitwise operators.
Check below for code.
0
// Half Adder logic - Recursive approach
// Not my implementation
int add(int a,int b)
{
int sum,carry;
if (b == 0)
return a;
sum = a ^ b; // add without carrying
carry = (a & b) << 1; // carry, but don’t add
return add(sum,carry); // recurse
}
0
//let the two numbers are :
int n1,n2;
//then
while(n1--)
n2=-(-n2-1);
//👉n2 is the required sum
//without using +or++ operator
court<<n2;