📜  在C / C++中将两个数字相加的8种不同方法

📅  最后修改于: 2021-04-23 22:16:49             🧑  作者: Mango

给定两个数字AB ,任务是找到给定两个数字的总和。
例子:

方法1 –使用加法运算符:在这里,只需在两个数字之间使用加法运算符,并打印数字的总和。

下面是上述方法的实现:

C++
// C++ program to add two number
// using addition operator
#include 
using namespace std;
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Return sum of A and B
    return A + B;
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    cout << "sum = " << addTwoNumber(A, B);
    return 0;
}


C++
// C++ program to add two number
// using subtraction operator
#include 
using namespace std;
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Return sum of A and B
    return A - (-B);
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    cout << "sum = " << addTwoNumber(A, B);
    return 0;
}


C++
// C++ program to add two number using
// increment/decrement operator
#include 
using namespace std;
 
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // When A is positive
    while (A > 0) {
        A--;
        B++;
    }
 
    // When A is negative
    while (A < 0) {
        A++;
        B--;
    }
 
    // Return sum of A and B
    return B;
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    cout << "sum = " << addTwoNumber(A, B);
    return 0;
}


C++
// C++ program to add two number
// using printf method
#include 
using namespace std;
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Return sum of A and B
    return printf("%*s%*s", A, "", B, "");
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    printf("sum = %d", addTwoNumber(A, B));
    return 0;
}


C++
// C++ program to add two number
// using half adder method
#include 
using namespace std;
 
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
 
    // Iterate till there is no carry
    while (B != 0) {
 
        // Carry now contains common
        // set bits of A and B
        int carry = A & B;
 
        // Sum of bits of A and B
        // where at least one of the
        // bits is not set
        A = A ^ B;
 
        // Carry is shifted by one so
        // that adding it to A gives
        // the required sum
        B = carry << 1;
    }
 
    return A;
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    printf("sum = %d", addTwoNumber(A, B));
    return 0;
}


C++
// C++ program to add two number
// using log and exponential
#include 
using namespace std;
 
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Return sum of A and B
    return log(exp(A) * exp(B));
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    printf("sum = %d", addTwoNumber(A, B));
    return 0;
}


C++
// C++ program to add two number
// using Recursion
#include 
 
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Base Case
    if (!A)
        return B;
 
    // Recursive Call
    else
        return addTwoNumber((A & B) << 1, A ^ B);
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    printf("sum = %d", addTwoNumber(A, B));
    return 0;
}


C++
// CPP program for above approach
#include 
using namespace std;
 
class A
{
    int a, b, sum;
 
public:
    A(int x, int y)
    {
        a = x;
        b = y;
    }
     
    // Using this pointer to
    // access variable
    void calcSum()
    {
      sum = this->a + this->b;
    }
 
    void showSum()
    {
      cout << "Sum is: " << sum << endl;
    }
};
 
// Driver Code
int main()
{
    int x, y;
    cout << "Enter Two Numbers: ";
    x = 4, y = 5;
    A a(x, y);
    a.calcSum();
    a.showSum();
    return 0;
}


输出
sum = 15

方法2 –使用减法运算符:在这里,只需在两个数字之间使用减法运算符两次,以便减号-减号乘以并产生+运算符,然后返回数字的和。

下面是上述方法的实现:

C++

// C++ program to add two number
// using subtraction operator
#include 
using namespace std;
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Return sum of A and B
    return A - (-B);
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    cout << "sum = " << addTwoNumber(A, B);
    return 0;
}
输出
sum = 15

方法3 –使用递增/递减运算符这里使用递增/递减运算符,当一个数字递减为零时,而另一个数字在第一个数字递减一个时递增一,则返回第二个数字。

下面是上述方法的实现:

C++

// C++ program to add two number using
// increment/decrement operator
#include 
using namespace std;
 
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // When A is positive
    while (A > 0) {
        A--;
        B++;
    }
 
    // When A is negative
    while (A < 0) {
        A++;
        B--;
    }
 
    // Return sum of A and B
    return B;
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    cout << "sum = " << addTwoNumber(A, B);
    return 0;
}
输出
sum = 15

方法4 –使用printf()方法:在此, “%* s”说明符将变量的打印值,变量时间的值以及printf返回屏幕上打印的字符。

下面是上述方法的实现:

C++

// C++ program to add two number
// using printf method
#include 
using namespace std;
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Return sum of A and B
    return printf("%*s%*s", A, "", B, "");
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    printf("sum = %d", addTwoNumber(A, B));
    return 0;
}
输出
sum = 15

方法5 –使用Half Adder方法可以通过对两个位执行按位XOR(^)来获得两个位的总和。进位可以通过对两个位进行按位AND(&)获得。
上面是简单的Half Adder逻辑,可用于将2个单个位相加。我们可以将此逻辑扩展为整数。如果x和y在同一位置没有设置位,则x和y的按位XOR(^)得出x和y的总和。为了合并公共置位,使用按位与(&)。 x和y的按位与运算得到所有进位。我们计算(x&y)<< 1并将其添加到x ^ y中以获得所需的结果。

下面是上述方法的实现:

C++

// C++ program to add two number
// using half adder method
#include 
using namespace std;
 
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
 
    // Iterate till there is no carry
    while (B != 0) {
 
        // Carry now contains common
        // set bits of A and B
        int carry = A & B;
 
        // Sum of bits of A and B
        // where at least one of the
        // bits is not set
        A = A ^ B;
 
        // Carry is shifted by one so
        // that adding it to A gives
        // the required sum
        B = carry << 1;
    }
 
    return A;
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    printf("sum = %d", addTwoNumber(A, B));
    return 0;
}
输出
sum = 15

方法6 –使用指数和对数:想法是找到给定两个数字的指数并打印结果的对数。

下面是上述方法的实现:

C++

// C++ program to add two number
// using log and exponential
#include 
using namespace std;
 
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Return sum of A and B
    return log(exp(A) * exp(B));
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    printf("sum = %d", addTwoNumber(A, B));
    return 0;
}
输出
sum = 15

方法7 –使用递归

  1. 获取要计算总和的数字A和B。
  2. 基本情况:如果A大于0,则返回B。
if(A > 0) {
   return B;
}

       3 。递归调用:将A更新为(A&B)<< 1,将B更新为A ^ B,然后递归调用更新后的值。

recursive_function((A & B) << 1, A ^ B);

下面是上述方法的实现:

C++

// C++ program to add two number
// using Recursion
#include 
 
// Function to return sum
// of two number
int addTwoNumber(int A, int B)
{
    // Base Case
    if (!A)
        return B;
 
    // Recursive Call
    else
        return addTwoNumber((A & B) << 1, A ^ B);
}
 
// Driver Code
int main()
{
    // Given two number
    int A = 4, B = 11;
 
    // Function call
    printf("sum = %d", addTwoNumber(A, B));
    return 0;
}
输出
sum = 15

方法8使用此指针

从用户那里获得两个数字x和y,任务是使用此指针找到给定的两个数字的和。

例子:

这可以通过使用此指针访问对象的数据成员并在它们之间执行加法操作来完成。

C++

// CPP program for above approach
#include 
using namespace std;
 
class A
{
    int a, b, sum;
 
public:
    A(int x, int y)
    {
        a = x;
        b = y;
    }
     
    // Using this pointer to
    // access variable
    void calcSum()
    {
      sum = this->a + this->b;
    }
 
    void showSum()
    {
      cout << "Sum is: " << sum << endl;
    }
};
 
// Driver Code
int main()
{
    int x, y;
    cout << "Enter Two Numbers: ";
    x = 4, y = 5;
    A a(x, y);
    a.calcSum();
    a.showSum();
    return 0;
}
输出
Enter Two Numbers: Sum is: 9