C++ Programs to Add Two Numbers

How to add two numbers in C++

In this short tutorial, I will show you adding two numbers in C++.

  • First program: Adds two variable values
  • Second Program: User is asked to enter two numbers to get the sum
  • Third Program: Class is used with two functions to add user entered two numbers
  • and more

C++ First program to add to variable values

  1. We have three variables i, j, and sum
  2. Variable i and j are assigned int values
  3. The next line adds the values of variable i and j and their toal is assigned to variable sum.

The code:

#include <iostream>

using namespace std;




int main() {

  int i, j, sum;

  i = 50;

  j = 75;

  sum = i + j;

  cout << sum;

  cout << "Sum of Two Numbers = " << sum;




  return 0;

}

Taking user input and adding two numbers program

In this program:

  • We ask the user to enter two int numbers
  • We assigned the entered numbers to two variables i and j
  • The values of two variables are added and the sum is assigned to the variable sum

The code:

#include <iostream>

using namespace std;




int main() {




  int i, j, sum;




  cout << "Please enter two numbers\n ";




  cin >> i >> j; //Taking user input




  sum = i+ j;




  // prints sum

  cout << "Sum of Two Numbers = " << sum;




  return 0;

}

CPP-add-two-input

Adding two real numbers program

The following program adds two real numbers (float) after taking the user input.

  • We declared three variables of float type
  • Ask the user to enter two real numbers
  • Get the sum and display it on the screen

The program

#include <iostream>

using namespace std;




int main() {




  float i, j, sum; //Declaring three float variables




  cout << "Please enter two real numbers\n ";




  cin >> i >> j; //Taking user input for real numbers




  sum = i+ j;




  // prints sum

  cout << "Sum of Two float Numbers = " << sum;




  return 0;

}

Sample Output:

CPP-add-two-real-nums

Using a function to add two numbers

Let us add two numbers while tasting how to write a simple function in C++.

  • Again, we will take two numbers as input from the user
  • The numbers are sent to the function (sumFunc)
  • The sumFunc function returns the sum of those two numbers
  • We will finally display the sum of two numbers on the screen

The code:

#include<iostream>

using namespace std;




int SumFunc(int, int);

int main()

{

    int i, j, sum;

    //Asking user to enter numbers

    cout<<"Please enter two int numbers: \n";

    cin>>i>>j;




    //Calling the SumFunc function

    sum = SumFunc(i, j);




    cout<<"The Sum of Two float Numbers = "<<sum;

    return 0;

}

int SumFunc(int x, int y)

{

    return (x+y);

}

A sample output:

CPP-add-two-Function

Using C++ Class to add two numbers

You might get the assignment to write a simple class in C++ for adding two numbers. So, I will show you how you can do it easily.

The code:

#include <iostream>

using namespace std;




class SumCls {

  int i, j;




public:

  void enterNum() {

    cout << "Please enter two numbers\n";

    cin >> i >> j;

  }




  void getSum() {

    cout << "Sum of Two Numbers = " << i + j;

  }




};




int main()

{

   SumCls m;




   m.enterNum();

   m.getSum();




   return 0;

}

A sample of the above code:

CPP-add-two-class

So what is in the code?

  • We created a class namely SumCls()
  • A function, enterNum is created in the class to take user input
  • The user entered two numbers that are assigned to i and j variables
  • A second function getSum() is created to add these two numbers
  • We also displayed the sum of two numbers in that function
  • In the main body, we simply called those two functions

Sum of two numbers with an array example

In this program,

  • We created an array of three elements
  • First two elements are assigned two int numbers that we want to get the sum
  • Third element stores the sum of the first two elements
  • We display the result i.e. third element

The program:

#include <iostream>

using namespace std;

int main() {

   int arrSum[3]; //Declaring an int type array of 3 elements




   arrSum[0]=10;

   arrSum[1]=20;

   arrSum[2]=arrSum[0] + arrSum[1];




   //Display result

   cout<<"Sum of "<<arrSum[0]<<" and "<<arrSum[1]<<" is \n"<<arrSum[2];

   return 0;

}

Output:

CPP-add-array

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!