Saturday, September 21, 2024
Google search engine
HomeData Modelling & AIAddition of two number using ‘-‘ operator

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;
}


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


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


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


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
?>


Javascript




<script>
  
// Javascript program to add two numbers using
// - operator.
  
// Function to add two numbers.
function add(a, b)
{
    return a - (-b);
}
  
// Driver code
var a = 2, b = 3;
  
document.write(add(a, b));
  
// This code is contributed by itsok
  
</script>


Output: 

5

 

Time complexity: O(1)
Auxiliary Space: O(1), As constant extra space is used.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments