Here are few ways using which addition can be performed without the use of arithmetic operator '+'.
1. Recursion
int add(int, int);
int main() {
int num1, num2;
printf("\nEnter the two Numbers : ");
scanf("%d %d", &num1, &num2);
printf("\nAddition of two num is : %d", add(num1, num2));
return (0);
}
int add(int num1, int num2) {
if (!num1)
return num2;
else
return add((num1 & num2) << 1, num1 ^ num2);
}
2. Looping 1
int main() {
int num1 = 10, num2 = 5, i;
while (num2 > 0) {
num1++;
num2--;
}
printf("%d", num1);
return (0);
}
3. Looping 2
int main() {
int num1 = 10, num2 = 5, i;
while (num2--) {
num1++;
}
printf("Sum is : %d", num1);
return (0);
}
4. Looping 3
int sum(int, int);
int main() {
int a, b;
printf("Enter the two Numbers: ");
scanf("%d %d", &a, &b);
printf("Addition of two num. is : %d", add(a, b));
return(0);
}
int add(int num1, int num2) {
int i;
for (i = 0; i < num2; i++)
num1++;
return num1;
}
5. Using Subtraction for Addition!
int main() {
int num1 = 10, num2 = 5;
num1 = num1 - (-num2);
printf("Sum is : %d",num1);
return (0);
}
I hope this note would be helpful to all. Do share your views and comments.
Note: Header files are not included in above code.