Open In App

# To find sum of two numbers without using any operator

Write a program to find sum of positive integers without using any operator. Only use of printf() is allowed. No other library function can be used.

Solution
It’s a trick question. We can use printf() to find sum of two numbers as printf() returns the number of characters printed. The width field in printf() can be used to find the sum of two numbers. We can use ‘*’ which indicates the minimum width of output. For example, in the statement “printf(“%*d”, width, num);”, the specified ‘width’ is substituted in place of *, and ‘num’ is printed within the minimum width specified. If number of digits in ‘num’ is smaller than the specified ‘width’, the output is padded with blank spaces. If number of digits are more, the output is printed as it is (not truncated). In the following program, add() returns sum of x and y. It prints 2 spaces within the width specified using x and y. So total characters printed is equal to sum of x and y. That is why add() returns x+y.

## C++

 `#include ``using` `namespace` `std;`` ` `int` `add(``int` `x, ``int` `y)``{``    ``return` `printf``(``"%*c%*c"``, x, ``' '``, y, ``' '``);``}` `// Driver code``int` `main()``{``    ``printf``(``"Sum = %d"``, add(3, 4));``    ``return` `0;``}` `// This code is contributed by shubhamsingh10`

## C

 `#include ` `int` `add(``int` `x, ``int` `y)``{``    ``return` `printf``(``"%*c%*c"``, x, ``' '``, y, ``' '``);``}` `// Driver code``int` `main()``{``    ``printf``(``"Sum = %d"``, add(3, 4));``    ``return` `0;``}`

## Java

 `public` `class` `Add``{``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `x = ``3``, y = ``4``;``        ``System.out.print(``"Sum = "` `+ add(x, y));``    ``}`` ` `    ``public` `static` `int` `add(``int` `x, ``int` `y)``    ``{``        ``String str = String.format(``"%"` `+ x + ``"c%"` `+ y + ``"c"``, ``' '``, ``' '``);``        ``return` `str.length();``    ``}``}`

## Python3

 `# Python code for the above approach``def` `add(x, y) :``    ` `    ``return` `(x ``+` `y);``   ` `# Driver code``if` `__name__ ``=``=` `"__main__"``:``    ` `    ``print``(``"Sum = "``, add(``3``, ``4``))` `    ``# This code is contributed by splvel62.`

## C#

 `using` `System;` `namespace` `Add``{``    ``class` `Program``    ``{``        ``static` `void` `Main(``string``[] args)``        ``{``            ``int` `x = 3, y = 4;``            ``Console.WriteLine(``"Sum = "` `+ Add(x, y));``        ``}` `        ``static` `int` `Add(``int` `x, ``int` `y)``        ``{``            ``string` `str = ``string``.Format(``"{0,"` `+ x + ``"}{1,"` `+ y + ``"}"``, ``' '``, ``' '``);``            ``return` `str.Length;``        ``}``    ``}``}`

## Javascript

 `function` `add(x, y)``{``    ``return` `console.log(``"%*c%*c"``, x, ``' '``, y, ``' '``);``}` `// Driver code``function` `main()``{``    ``console.log(``"Sum = %d"``, add(3, 4));``    ``return` `0;``}` `main();` `// This code is contributed by factworx412`

Output:

`Sum = 7`

Time Complexity: O(1)
Auxiliary Space: O(1)

The output is seven spaces followed by “Sum = 7”. We can avoid the leading spaces by using carriage return. Thanks to krazyCoder and Sandeep for suggesting this. The following program prints output without any leading spaces.

## C++

 `#include ``using` `namespace` `std;` `int` `add(``int` `x, ``int` `y)``{``    ``return` `printf``(``"%*c%*c"``, x, ``'\r'``, y, ``'\r'``);``}` `// Driver code``int` `main()``{``    ``printf``(``"Sum = %d"``, add(3, 4));``    ``return` `0;``}` `// This code is contributed by shubhamsingh10`

## C

 `#include ` `int` `add(``int` `x, ``int` `y)``{``    ``return` `printf``(``"%*c%*c"``, x, ``'\r'``, y, ``'\r'``);``}` `// Driver code``int` `main()``{``    ``printf``(``"Sum = %d"``, add(3, 4));``    ``return` `0;``}`

## Java

 `class` `GFG {` `    ``static` `int` `add(``int` `x, ``int` `y) {``        ``return` `(x + y);``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args) {``        ``System.out.printf(``"Sum = %d"``, add(``3``, ``4``));``    ``}``}` `// This code is contributed by Rajput-Ji`

## Python3

 `# Python program for the above approach``def` `add(x, y) :``    ` `    ``return` `(x ``+` `y);``    `  `# driver code``print``(``"Sum = "``, add(``3``, ``4``));` `# This code is contributed by sanjoy_62`

## C#

 `// C# program for the above approach``using` `System;` `public` `class` `GFG {` `  ``static` `int` `add(``int` `x, ``int` `y)``  ``{``    ``return` `(x + y);``  ``}`  `  ``// Driver Code``  ``public` `static` `void` `Main(String[] args) {` `    ``Console.WriteLine(``"Sum = "` `+ add(3, 4));``  ``}``}` `// This code is contributed by code_hunt.`

## Javascript

 ``

Output:

`      Sum = 7`

Time Complexity: O(1)

Auxiliary Space: O(1)

Another Method :

## C++

 `#include ``using` `namespace` `std;` `int` `main()``{``    ``int` `a = 10, b = 5;``    ``if` `(b > 0) {``        ``while` `(b > 0) {``            ``a++;``            ``b--;``        ``}``    ``}``    ``if` `(b < 0) { ``// when 'b' is negative``        ``while` `(b < 0) {``            ``a--;``            ``b++;``        ``}``    ``}``    ``cout << ``"Sum = "` `<< a;``    ``return` `0;``}` `// This code is contributed by SHUBHAMSINGH10``// This code is improved & fixed by Abhijeet Soni.`

## C

 `#include ` `int` `main()``{``    ``int` `a = 10, b = 5;``    ``if` `(b > 0) {``        ``while` `(b > 0) {``            ``a++;``            ``b--;``        ``}``    ``}``    ``if` `(b < 0) { ``// when 'b' is negative``        ``while` `(b < 0) {``            ``a--;``            ``b++;``        ``}``    ``}``    ``printf``(``"Sum = %d"``, a);``    ``return` `0;``}` `// This code is contributed by Abhijeet Soni`

## Java

 `// Java code``class` `GfG {` `    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `a = ``10``, b = ``5``;``        ``if` `(b > ``0``) {``            ``while` `(b > ``0``) {``                ``a++;``                ``b--;``            ``}``        ``}``        ``if` `(b < ``0``) { ``// when 'b' is negative``            ``while` `(b < ``0``) {``                ``a--;``                ``b++;``            ``}``        ``}``        ``System.out.println(``"Sum is: "` `+ a);``    ``}``}` `// This code is contributed by Abhijeet Soni`

## Python3

 `# Python 3 Code` `if` `__name__ ``=``=` `'__main__'``:``    ` `    ``a ``=` `10``    ``b ``=` `5` `    ``if` `b > ``0``:``        ``while` `b > ``0``:``            ``a ``=` `a ``+` `1``            ``b ``=` `b ``-` `1``    ``if` `b < ``0``:``        ``while` `b < ``0``:``            ``a ``=` `a ``-` `1``            ``b ``=` `b ``+` `1``    ` `    ``print``(``"Sum is: "``, a)` `# This code is contributed by Akanksha Rai``# This code is improved & fixed by Abhijeet Soni`

## C#

 `// C# code``using` `System;` `class` `GFG {``    ``static` `public` `void` `Main()``    ``{``        ``int` `a = 10, b = 5;``        ``if` `(b > 0) {``            ``while` `(b > 0) {``                ``a++;``                ``b--;``            ``}``        ``}``        ``if` `(b < 0) { ``// when 'b' is negative``            ``while` `(b < 0) {``                ``a--;``                ``b++;``            ``}``        ``}``        ``Console.Write(``"Sum is: "` `+ a);``    ``}``}` `// This code is contributed by Tushil``// This code is improved & fixed by Abhijeet Soni.`

## PHP

 ` 0) {``while``(``\$b` `> 0)``{``    ``\$a``++;``    ``\$b``--;``}``}` `if` `(``\$b` `< 0) {``while``(``\$b` `< 0)``{``    ``\$a``--;``    ``\$b``++;``}``}`  `echo` `"Sum is: "``, ``\$a``;` `// This code is contributed by Dinesh``// This code is improved & fixed by Abhijeet Soni.``?>`

## Javascript

 ``

Output:

`sum = 15`

Time Complexity: O(b)
Auxiliary Space: O(1)

## Approach: Bit Manipulation

Steps:

1. Calculate the sum of the numbers without taking into account the carry.
2. Calculate the carry using bitwise AND and shift left operator.
3. Add the carry to the sum.
4. Repeat steps 1-3 until there is no carry left.

## C++

 `// C++ program for the above approach``#include ` `// Function to add two number without``// operators``int` `add_without_operator(``int` `a, ``int` `b)``{` `    ``while` `(b != 0) {` `        ``// Calculate sum without carry``        ``int` `sum = a ^ b;` `        ``// Calculate carry``        ``int` `carry = (a & b) << 1;` `        ``// Add sum and carry``        ``a = sum;``        ``b = carry;``    ``}` `    ``return` `a;``}` `// Driver Code``int` `main()``{``    ``int` `a = 5;``    ``int` `b = 7;``    ``int` `result = add_without_operator(a, b);``    ``std::cout << ``"The sum of "` `<< a << ``" and "` `<< b``              ``<< ``" is: "` `<< result << std::endl;` `    ``return` `0;``}`

## Java

 `public` `class` `AddWithoutOperator {``    ``public` `static` `int` `add(``int` `a, ``int` `b)``    ``{``        ``while` `(b != ``0``) {``            ``int` `sum = a ^ b; ``// XOR operation to calculate``                             ``// sum without carry``            ``int` `carry``                ``= (a & b)``                  ``<< ``1``; ``// AND and left shift operation to``                        ``// calculate carry``            ``a = sum;``            ``b = carry;``        ``}``        ``return` `a;``    ``}` `    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `a = ``5``;``        ``int` `b = ``7``;``        ``int` `result = add(a, b);``        ``System.out.println(``"The sum of "` `+ a + ``" and "` `+ b``                           ``+ ``" is: "` `+ result);``    ``}``}`

## Python3

 `def` `add_without_operator(a: ``int``, b: ``int``) ``-``> ``int``:``    ``while` `b !``=` `0``:``        ``# Calculate sum without carry``        ``sum` `=` `a ^ b``        ` `        ``# Calculate carry``        ``carry ``=` `(a & b) << ``1``        ` `        ``# Add sum and carry``        ``a ``=` `sum``        ``b ``=` `carry``    ` `    ``return` `a` `a ``=` `5``b ``=` `7``result ``=` `add_without_operator(a, b)``print``(f``"The sum of {a} and {b} is: {result}"``)`

## C#

 `using` `System;` `class` `Program``{``    ``// Function to add two number without operators``    ``static` `int` `AddWithoutOperator(``int` `a, ``int` `b)``    ``{``        ``while` `(b != 0)``        ``{``            ``// Calculate sum without carry``            ``int` `sum = a ^ b;` `            ``// Calculate carry``            ``int` `carry = (a & b) << 1;` `            ``// Add sum and carry``            ``a = sum;``            ``b = carry;``        ``}` `        ``return` `a;``    ``}` `    ``// Driver Code``    ``static` `void` `Main(``string``[] args)``    ``{``        ``int` `a = 5;``        ``int` `b = 7;``        ``int` `result = AddWithoutOperator(a, b);``        ``Console.WriteLine(``"The sum of {0} and {1} is: {2}"``, a, b, result);``    ``}``}`

## Javascript

 `function` `addWithoutOperator(a, b) {``while` `(b !== 0) {``// Calculate sum without carry``let sum = a ^ b;``// Calculate carry``let carry = (a & b) << 1;` `// Add sum and carry``a = sum;``b = carry;``}` `return` `a;``}` `// Driver Code``let a = 5;``let b = 7;``let result = addWithoutOperator(a, b);``console.log(`The sum of \${a} and \${b} is: \${result}`);`

Output

`The sum of 5 and 7 is: 12`

Time Complexity: O(log N), where N is the maximum number of bits in a or b.
Auxiliary Space: O(1)