📌  相关文章
📜  使用’-‘运算符将两个数字相加

📅  最后修改于: 2021-05-31 22:13:56             🧑  作者: Mango

任务是使用’-‘运算符两个数字相加

例子:

Input : 2 3
Output : 5

Input : 10 20
Output : 30

这个想法很简单,我们从a减去-b。

C++
// CPP program to add two numbers using
// - operator.
#include 
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


Javascript


输出:
5
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”