If you meant without using any arithmetic operators , then this should work :
- #include<stdio. h>
- int main(){
- int num1 = 12, num2 = 25;
- // will iterate till theres no carry.
- while (num2) {
- int carry = num1 & num2; // carry bit obtained by simple AND.
- num1 = num1 ^ num2; // sum by XOR.
- num2 = carry << 1;
Similarly one may ask, how can I add two numbers without using operator in Java?
1.Iterative Solution to add two integers without using Arithmetic operator
- int carry = (a & b) ; //CARRY is AND of two bits.
- a = a ^b; //SUM of two bits is A XOR B.
- b = carry << 1; //shifts carry to 1 bit to calculate sum. }
- return a; }
Also Know, how do you multiply two numbers without using an operator?
- { public static int multiply(int a, int b)
- // if both numbers are negative, make both numbers. // positive since the result will be positive anyway.
- } // if only `a` is negative, make it positive.
- a = -a;
- if (b < 0)
- // initialize result by 0.
- // if `b` is odd, add `b` to the result.
- b = b >> 1; // divide `b` by 2.
Considering this, how can I add two numbers without using C?
Add two numbers without using the addition operator | 5 methods
- Using subtraction operator. int add(int a, int b) {
- Repeated Addition/Subtraction using --/++ operator. #include <iostream>
- Using printf() function. This method makes use of two facts:
- Half adder logic.
- Using logarithm and exponential function.
How do you add two numbers using an operator?
In programming, the ++ operator is the increment operator that increases the value of the operand by 1. We can add two numbers using this operator by adding 1 to the number a, b number of times. Explanation − adding 1 to 31 four times, sums up to 31 +1+1+1+1 = 35.
