# 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.

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

*chevron_right*

*filter_none*

**Output:**

Sum = 7

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.

`#include<stdio.h> ` ` ` `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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Sum = 7

Another Method :

## C

`#include<stdio.h> ` ` ` `int` `main() ` `{ ` `int` `a=10,b=5,sum; ` `while` `(b>0){ ` `a++; ` `b--; ` `} ` `printf` `(` `"Sum=%d"` `,a); ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code ` `class` `GfG { ` ` ` `public` `static` `void` `main(String[] args) ` `{ ` `int` `a=` `10` `,b=` `5` `,sum; ` `while` `(b>` `0` `){ ` `a++; ` `b--; ` `} ` `System.out.println(a); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 Code ` ` ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `a ` `=` `10` `; b ` `=` `5` `; ` `sum` `; ` ` ` `while` `(b > ` `0` `): ` ` ` ` ` `a ` `=` `a ` `+` `1` `; ` ` ` `b ` `=` `b ` `-` `1` `; ` ` ` ` ` `print` `(` `"Sum ="` `, a); ` ` ` `# This code is contributed ` `# by Akanksha Rai ` |

*chevron_right*

*filter_none*

## C#

`// C# code ` `using` `System; ` ` ` `public` `class` `GFG{ ` ` ` `static` `public` `void` `Main (){ ` ` ` `int` `a=10,b=5; ` ` ` `while` `(b>0){ ` ` ` `a++; ` ` ` `b--; ` ` ` `} ` ` ` `Console.Write(a); ` ` ` `} ` `//This code is contributed by Tushil. ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Code ` `$a` `= 10; ` `$b` `= 5; ` ` ` `while` `(` `$b` `> 0) ` `{ ` ` ` `$a` `++; ` ` ` `$b` `--; ` `} ` ` ` `echo` `"Sum = "` `, ` `$a` `; ` ` ` `// This code is contributed by Dinesh. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

sum = 15

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

## Recommended Posts:

- Program to find remainder without using modulo or % operator
- Program to Find the Largest Number using Ternary Operator
- Find two numbers whose sum and GCD are given
- Given two numbers a and b find all x such that a % x = b
- Find k numbers which are powers of 2 and have sum N | Set 1
- Find two numbers with sum and product both same as N
- Find the sum of the all amicable numbers up to N
- Program to find LCM of 2 numbers without using GCD
- Find LCM of rational numbers
- Program to find LCM of two numbers
- Program to find GCD or HCF of two numbers
- Find two prime numbers with given sum
- Find the sum of first N odd Fibonacci numbers
- Find max of two Rational numbers
- Find if n can be written as product of k numbers