Addition of two number using ‘-‘ operator
The task is to Add two number using ‘-‘ operator.
Examples:
Input : 2 3 Output : 5 Input : 10 20 Output : 30
The idea is simple, we subtract -b from a.
C++
// CPP program to add two numbers using // - operator. #include <bits/stdc++.h> using namespace std; // function to add two numbers. int add(int a, int b) { return a - (-b); } // Driver code int main() { int a = 2, b = 3; cout << add(a, b) << endl; return 0; } |
chevron_right
filter_none
Java
// Java program to add // two numbers using // - operator. import java.io.*; class GFG { // function to add two numbers. static int add(int a, int b) { return a - (-b); } // Driver code public static void main (String[] args) { int a = 2, b = 3; System.out.print(add(a, b)); } } // This code is contributed // by chandan_jnu |
chevron_right
filter_none
Python3
# Python 3 program to add two numbers # using - operator. # Function to add two numbers def add(a, b): return (a - (-b)) # Driver code if __name__ == "__main__" : a = 2 b = 3 print(add(a, b)) # this code is contributed by Naman_Garg |
chevron_right
filter_none
C#
// C# program to add // two numbers using // - operator. class GFG { // function to add two numbers. static int add(int a, int b) { return a - (-b); } // Driver code static void Main() { int a = 2, b = 3; System.Console.WriteLine(add(a, b)); } } // This code is contributed // by mits |
chevron_right
filter_none
PHP
<?php // PHP program to add two // numbers using - operator. // function to add two numbers. function add($a, $b) { return $a - (-$b); } // Driver code $a = 2; $b = 3; echo add($a, $b); // This code is contributed by mits ?> |
chevron_right
filter_none
Output:
5
Recommended Posts:
- Program for addition of two matrices
- dot (.) operator in C/C++
- sizeof operator in C
- Operator Overloading in C++
- Division without using '/' operator
- array::operator[ ] in C++ STL
- Types of Operator Overloading in C++
- C++ | Nested Ternary Operator
- Operator Precedence and Associativity in C
- Ternary Operator in Python
- Operator Functions in Python | Set 1
- Conditional or Ternary Operator (?:) in C/C++
- Increment (++) and Decrement (--) operator overloading in C++
- Implement *, - and / operations using only + arithmetic operator
- Can we use % operator on floating point numbers?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



