C++ programs to add two numbers

In this C++ programming article we will see some C++ programs to add two numbers. At first we will take two integers as input from user and then we will print the result after addition of that two numbers. We can also subtract one number form another using the same logic. See our addition and subtraction in C to get the knowledge about how we can perform addition or subtraction operations.

Now, let’s go ahead with code to add two numbers which are given by user.

Add two numbers program in C++

In this bellow program we will take two integers from user, then add them. After printing the result of addition we will also subtract them and print the result.

// c++ program to add and subtract two numbers

#include <iostream>
using namespace std;

int main(){
   int f, s, result, sub;

   cout << "Enter first integer here : ";
   cin >> f;
   cout << "Enter second integer here : ";
   cin >> s;

   result = f + s;

   cout <<"\nSum of " << f << " and " << s << " is = " << result << endl;

   sub = f - s;

   cout <<"\nSubtraction of " << f << " and " << s << " is = " << sub << endl;

   return 0;

Output of add numbers program


Add two numbers using a class in C++

The bellow program will and subtract the given numbers using a class named class_to_add. See the program code bellow.

// c++ program to add two number using a class

#include <iostream>
using namespace std;

class class_to_add{    // creating a class to add numbers
    int f, s;

    void take_input(){
      cout << "Enter two integers here to add them : \n";
      cin >> f >> s;

    void funct_to_add(){
      cout << "\nSum is = " << f + s << endl;
      cout << "Subtraction is = " << f - s << endl;


int main(){
    class_to_add x; // Creating an object here


    return 0;

Output of this program:

Enter two integers here to add them :
45 25

Sum is = 70
Subtraction is = 20

Previous pagePrint hello world

Next pageMultiply and divide