Addition of two number using ‘-‘ operator
Last Updated :
13 Mar, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int add( int a, int b)
{
return a - (-b);
}
int main()
{
int a = 2, b = 3;
cout << add(a, b) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int add( int a, int b)
{
return a - (-b);
}
public static void main (String[] args)
{
int a = 2 , b = 3 ;
System.out.print(add(a, b));
}
}
|
Python3
def add(a, b):
return (a - ( - b))
if __name__ = = "__main__" :
a = 2
b = 3
print (add(a, b))
|
C#
class GFG
{
static int add( int a, int b)
{
return a - (-b);
}
static void Main()
{
int a = 2, b = 3;
System.Console.WriteLine(add(a, b));
}
}
|
PHP
<?php
function add( $a , $b )
{
return $a - (- $b );
}
$a = 2;
$b = 3;
echo add( $a , $b );
?>
|
Javascript
<script>
function add(a, b)
{
return a - (-b);
}
var a = 2, b = 3;
document.write(add(a, b));
</script>
|
Time complexity: O(1)
Auxiliary Space: O(1), As constant extra space is used.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...