## C++ || Simple Math Using Int & Float This page will display the use of int and float data types. Note: you can (obviously) add as many int’s together as you wish, not just two.

==== ADDING TWO NUMBERS TOGETHER ====

To add two numbers together, you will have to first declare your variables by doing something like this.

``` Adding 2 Numbers Together C++ #include <iostream> using namespace std; int main() { int num1 = 0; int num2 = 0; int sum = 0; cout << "Please enter the first number: "; cin >> num1; cout << endl << "Please enter the second number: "; cin >> num2; // calculate the sum of the two numbers here sum = num1 + num2; cout << endl << "The sum of " <<num1<<" and "<<num2<< " is: "<< sum << endl; return 0; }// http://programmingnotes.freeweq.com 12345678910111213141516171819 #include <iostream>using namespace std;int main(){     int num1 = 0; int num2 = 0; int sum = 0;  cout << "Please enter the first number: "; cin >> num1; cout << endl << "Please enter the second number: "; cin >> num2;  // calculate the sum of the two numbers here sum = num1 + num2;  cout << endl << "The sum of " <<num1<<" and "<<num2<< " is: "<< sum << endl;    return 0;}// http://programmingnotes.freeweq.com ```

Notice in lines 5-7, I declared my variables, giving them a name. You can name your variables anything you want, with a rule of thumb as naming them something meaningful to your code (i.e avoid giving your variables arbitrary names like “x” or “y”). In line 15 the actual math process is taking place, storing the sum of “num1” and “num2” in a variable called “sum.” I also initialized my variables to zero. You should always initialize your variables.

the resulting code should give you the following output:

```Please enter the first number: 8 Please enter the second number: 24 The sum of 8 and 24 is: 32```

==== SUBTRACTING TWO NUMBERS ====

Subtracting two ints works the same way as the above code, and we would only need to edit the above code in one place to achieve that. In line 15, replace the addition symbol with a subtraction sign, and you should have something like this

``` Subtracting 2 Numbers Together C++ #include <iostream> using namespace std; int main() { int num1 = 0; int num2 = 0; int sum = 0; cout << "Please enter the first number: "; cin >> num1; cout << endl << "Please enter the second number: "; cin >> num2; // calculate the difference of the two numbers here sum = num1 - num2; cout << endl << "The difference between " <<num1<<" and "<<num2<< " is: "<< sum << endl; return 0; }// http://programmingnotes.freeweq.com 12345678910111213141516171819 #include <iostream>using namespace std;int main(){     int num1 = 0; int num2 = 0; int sum = 0;  cout << "Please enter the first number: "; cin >> num1; cout << endl << "Please enter the second number: "; cin >> num2;  // calculate the difference of the two numbers here sum = num1 - num2;  cout << endl << "The difference between " <<num1<<" and "<<num2<< " is: "<< sum << endl;    return 0;}// http://programmingnotes.freeweq.com ```

The above code should give you the following output

```Please enter the first number: 8 Please enter the second number: 24 The difference between 8 and 24 is: -16```

==== MULTIPLYING TWO NUMBERS ====

This can be achieved the same way as the 2 previous methods, simply by editing line 15, and replacing the designated math operator with the star symbol “*.”

``` Multiplying 2 Numbers Together C++ #include <iostream> using namespace std; int main() { int num1 = 0; int num2 = 0; int sum = 0; cout << "Please enter the first number: "; cin >> num1; cout << endl << "Please enter the second number: "; cin >> num2; // calculate the product of the two numbers here sum = num1 * num2; cout << endl << "The product of " <<num1<<" and "<<num2<< " is: "<< sum << endl; return 0; }// http://programmingnotes.freeweq.com 12345678910111213141516171819 #include <iostream>using namespace std;int main(){     int num1 = 0; int num2 = 0; int sum = 0;  cout << "Please enter the first number: "; cin >> num1; cout << endl << "Please enter the second number: "; cin >> num2;  // calculate the product of the two numbers here sum = num1 * num2;  cout << endl << "The product of " <<num1<<" and "<<num2<< " is: "<< sum << endl;    return 0;}// http://programmingnotes.freeweq.com ```

The above code should give you the following output

```Please enter the first number: 8 Please enter the second number: 24 The product of 8 and 24 is: 192```

==== DIVIDING TWO NUMBERS TOGETHER ====

This one is a little different from the other three. Before we would use int variables to store our data. In division, when you divide numbers together, sometimes they end in decimals. Int data types can not store decimal data (try it yourself and see), so here is where we use the float data type (you can also use the “double” data type here aswell).

So the resulting code will basically be the same as the other previous three, only instead of our variables being of type int, they will be of type float.

``` Dividing 2 Numbers Together C++ #include <iostream> using namespace std; int main() { int num1 = 0; int num2 = 0; float sum = 0; cout << "Please enter the first number: "; cin >> num1; cout << endl << "Please enter the second number: "; cin >> num2; // calculate the quotient of the two numbers here sum = num1 / num2; cout << endl << "The quotient of " <<num1<<" and "<<num2<< " is: "<< sum << endl; return 0; }// http://programmingnotes.freeweq.com 12345678910111213141516171819 #include <iostream>using namespace std;int main(){     int num1 = 0; int num2 = 0; float sum = 0;  cout << "Please enter the first number: "; cin >> num1; cout << endl << "Please enter the second number: "; cin >> num2;  // calculate the quotient of the two numbers here sum = num1 / num2;  cout << endl << "The quotient of " <<num1<<" and "<<num2<< " is: "<< sum << endl;    return 0;}// http://programmingnotes.freeweq.com ```

The above code should give the following output

```Please enter the first number: 8 Please enter the second number: 24 The quotient of 8 and 24 is: 0.333333```

==== MODULUS ====

If you wanted to capture the remainder of the quotient you calculated from the above code, you would use the modulus operator (%).

From the above code, you would only need to edit line 15, from division, to modulus.

``` Getting the Remainder of 2 Numbers C++ #include <iostream> using namespace std; int main() { int num1 = 0; int num2 = 0; float remainder = 0; cout << "Please enter the first number: "; cin >> num1; cout << endl << "Please enter the second number: "; cin >> num2; // find the remainder of the two numbers here remainder = num1 % num2; cout << endl << "The remainder of " <<num1<<" and "<<num2<< " is: "<< remainder << endl; return 0; }// http://programmingnotes.freeweq.com 12345678910111213141516171819 #include <iostream>using namespace std;int main(){     int num1 = 0; int num2 = 0; float remainder = 0;  cout << "Please enter the first number: "; cin >> num1; cout << endl << "Please enter the second number: "; cin >> num2;  // find the remainder of the two numbers here remainder = num1 % num2;  cout << endl << "The remainder of " <<num1<<" and "<<num2<< " is: "<< remainder << endl;    return 0;}// http://programmingnotes.freeweq.com ```

The above code should give the following output

```Please enter the first number: 24 Please enter the second number: 8 The remainder of 24 and 8 is: 0```