Add two numbers using ++ and/or —
Given two numbers, return sum of them without using operators + and/or -, and using ++ and/or –.
Examples:
Input: x = 10, y = 5 Output: 15 Input: x = 10, y = -5 Output: 10
We strongly recommend you to minimize your browser and try this yourself first
The idea is to do y times x++, if y is positive and do y times x– if y is negative.
C++
// C++ program to add two numbers using ++ #include <bits/stdc++.h> using namespace std; // Returns value of x+y without using + int add(int x, int y) { // If y is positive, y times add 1 to x while (y > 0 && y--) x++; // If y is negative, y times subtract 1 from x while (y < 0 && y++) x--; return x; } int main() { cout << add(43, 23) << endl; cout << add(43, -23) << endl; return 0; } |
chevron_right
filter_none
Java
// java program to add two numbers // using ++ public class GFG { // Returns value of x+y without // using + static int add(int x, int y) { // If y is positive, y times // add 1 to x while (y > 0 && y != 0) { x++; y--; } // If y is negative, y times // subtract 1 from x while (y < 0 && y != 0) { x--; y++; } return x; } // Driver code public static void main(String args[]) { System.out.println(add(43, 23)); System.out.println(add(43, -23)); } } // This code is contributed by Sam007. |
chevron_right
filter_none
Python3
# python program to add two # numbers using ++ # Returns value of x + y # without using + def add(x, y): # If y is positive, y # times add 1 to x while (y > 0 and y): x = x + 1 y = y - 1 # If y is negative, y # times subtract 1 # from x while (y < 0 and y) : x = x - 1 y = y + 1 return x # Driver code print(add(43, 23)) print(add(43, -23)) # This code is contributed # by Sam007. |
chevron_right
filter_none
C#
// C# program to add two numbers // using ++ using System; public class GFG { // Returns value of x+y without // using + static int add(int x, int y) { // If y is positive, y times // add 1 to x while (y > 0 && y != 0) { x++; y--; } // If y is negative, y times // subtract 1 from x while (y < 0 && y != 0) { x--; y++; } return x; } // Driver code public static void Main() { Console.WriteLine(add(43, 23)); Console.WriteLine(add(43, -23)); } } // This code is contributed by Sam007. |
chevron_right
filter_none
PHP
<?php // PHP program to add two // numbers using ++ // Returns value of // x+y without using + function add($x, $y) { // If y is positive, // y times add 1 to x while ($y > 0 && $y--) $x++; // If y is negative, // y times subtract // 1 from x while ($y < 0 && $y++) $x--; return $x; } // Driver Code echo add(43, 23), "\n"; echo add(43, -23), "\n"; // This code is contributed by ajit. ?> |
chevron_right
filter_none
Output:
66 20
Thanks to Gaurav Ahirwar for suggesting above solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Count numbers which can be constructed using two numbers
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Count numbers which are divisible by all the numbers from 2 to 10
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Sum of first n even numbers
- Tribonacci Numbers
- Happy Numbers
- Sum of all even numbers in range L and R
- Dudeney Numbers
- Undulating numbers
- Sum of cubes of first n even numbers

