How one can add two numbers in C++
On this brief tutorial, I’ll present you including two numbers in C++.
- First program: Provides two variable values
- Second Program: Consumer is requested to enter two numbers to get the sum
- Third Program: Class is used with two features so as to add person entered two numbers
- and extra
C++ First program so as to add to variable values
- We now have three variables i, j, and sum
- Variable i and j are assigned int values
- The subsequent line provides the values of variable i and j and their toal is assigned to variable sum.
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#embody <iostream>
utilizing namespace std;
int most important() {
int i, j, sum;
i = 50;
j = 75;
sum = i + j;
cout << sum;
cout << “Sum of Two Numbers = “ << sum;
return 0;
} |
Taking person enter and including two numbers program
On this program:
- We ask the person to enter two int numbers
- We assigned the entered numbers to 2 variables i and j
- The values of two variables are added and the sum is assigned to the variable sum
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#embody <iostream>
utilizing namespace std;
int most important() {
int i, j, sum;
cout << “Please enter two numbersn “;
cin >> i >> j; //Taking person enter
sum = i+ j;
// prints sum
cout << “Sum of Two Numbers = “ << sum;
return 0;
} |
Including two actual numbers program
The next program provides two actual numbers (float) after taking the person enter.
- We declared three variables of float kind
- Ask the person to enter two actual numbers
- Get the sum and show it on the display
This system
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#embody <iostream>
utilizing namespace std;
int most important() {
float i, j, sum; //Declaring three float variables
cout << “Please enter two actual numbersn “;
cin >> i >> j; //Taking person enter for actual numbers
sum = i+ j;
// prints sum
cout << “Sum of Two float Numbers = “ << sum;
return 0;
} |
Pattern Output:
Utilizing a operate so as to add two numbers
Allow us to add two numbers whereas tasting the best way to write a easy operate in C++.
- Once more, we’ll take two numbers as enter from the person
- The numbers are despatched to the operate (sumFunc)
- The sumFunc operate returns the sum of these two numbers
- We’ll lastly show the sum of two numbers on the display
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#embody<iostream>
utilizing namespace std;
int SumFunc(int, int);
int most important()
{
int i, j, sum;
//Asking person to enter numbers
cout<<“Please enter two int numbers: n”;
cin>>i>>j;
//Calling the SumFunc operate
sum = SumFunc(i, j);
cout<<“The Sum of Two float Numbers = “<<sum;
return 0;
}
int SumFunc(int x, int y)
{
return (x+y);
} |
A pattern output:
Utilizing C++ Class so as to add two numbers
You may get the task to jot down a easy class in C++ for including two numbers. So, I’ll present you ways you are able to do it simply.
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#embody <iostream>
utilizing namespace std;
class SumCls {
int i, j;
public:
void enterNum() {
cout << “Please enter two numbersn”;
cin >> i >> j;
}
void getSum() {
cout << “Sum of Two Numbers = “ << i + j;
}
};
int most important()
{
SumCls m;
m.enterNum();
m.getSum();
return 0;
} |
A pattern of the above code:
So what’s within the code?
- We created a category particularly SumCls()
- A operate, enterNum is created within the class to take person enter
- The person entered two numbers which can be assigned to i and j variables
- A second operate getSum() is created so as to add these two numbers
- We additionally displayed the sum of two numbers in that operate
- In the primary physique, we merely referred to as these two features
Sum of two numbers with an array instance
On this program,
- We created an array of three components
- First two components are assigned two int numbers that we need to get the sum
- Third component shops the sum of the primary two components
- We show the end result i.e. third component
This system:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#embody <iostream>
utilizing namespace std;
int most important() {
int arrSum[3]; //Declaring an int kind array of three components
arrSum[0]=10;
arrSum[1]=20;
arrSum[2]=arrSum[0] + arrSum[1];
//Show end result
cout<<“Sum of “<<arrSum[0]<<” and “<<arrSum[1]<<” is n”<<arrSum[2];
return 0;
} |
Output: