How to add two numbers in C++
In this short tutorial, I will show you how to add 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
1. C++ First program to add to variable values
- We have three variables i, j, and sum
- Variable i and j are assigned int values
- The next line adds the values of variables i and j; their total is assigned to the 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 << "The sum of two numbers = " << sum; return 0; }
Output:
2. 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; }
Sample output:
3. 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:
4. 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:
5. 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:
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
6. Sum of two numbers with an array example
In this program,
- We created an array of three elements
- The 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:
30