C++ Program to Add Two Numbers | write program Add numbers Using class in C++




C++ program to add two numbers


C++ program to add two numbers.

C++ programming code

----------------------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
   int n1, n2, result;
 
   cout << "Enter two integers to add : \n";
   cin >> n1 >> n2;
   result = n1 + n2;
   cout <<"Sum of the numbers: " << result << endl;
 
   return 0;
}

C++ addition program using class

----------------------------------------------------------------------------

#include <iostream>
using namespace std;
class Mathematics {
  int n1, n2;
public:
  void input() {
    cout << "Input two integers\n";
    cin >> n1 >> n2;
  }
  void add() {
    cout << "Result: " << n1 + n2;
  }
};
int main()
{
   Mathematics m; // Creating an object of the class
   m.input();   // calling input function of the class
   m.add();    // calling add function of the class
   return 0;
}

Post a Comment

0 Comments