You are here

C++ program to add two numbers

C++ program to add two numbers.

C++ programming code

#include <iostream>

using namespace std;

int main()
{
   int a, b, c;
   
   cout << "Enter two integers to add\n";
   cin >> a >> b;

   c = a + b;
   cout <<"Sum of the numbers: " << c << endl;
   
   return 0;
}

C++ addition program using class

#include <iostream>

using namespace std;

class Mathematics {
  int x, y;

public:
  void input() {
    cout << "Input two integers\n";
    cin >> x >> y;
  }

  void add() {
    cout << "Result: " << x + y;
  }

};

int main()
{
   Mathematics m; // Creating an object of the class

   m.input();
   m.add();

   return 0;
}

We create Mathematics class with two functions input and add. Function input is used to get two integers from a user, and function add performs the addition and displays the result. Similarly, you can create more functions to subtract, multiply, divide.