C++ Tutorial III – Using Simple Mathematics
4 minute read
To understand it better, you should first be familiar with everyday C++ Math Functions. In the following tutorial, look over the following 5 examples and look at how the code changes. We’ll look at Addition, Subtraction, Multiplication, and Division.
ADDITION
*The Math is bolded while the rest is in regular font
#include </p>
using namespace std;
int main()
{
int x;
int y;
cout<<“Have You Ever Wanted to Add 2 Numbers? Of course! You can do that right here! Enter your first number!”;
cin>> x;
cout<<“Enter your second number!”;
cin>> y;
cout<<“The sum of the two numbers is ” << x+y;
system(“pause”);
return 0;
}
</td> </tr> </tbody> </table> </td> </tr> </tbody> </table>
Pretty easy, huh? All you really need to know is how to place a variable within the quotations. But that isn’t too hard is it?
SUBTRACTION
Our next function is subtraction and it is not any more difficult. If you must see what the code looks like, look below. Notice that you show the variables the same way you do with addition but you take out the addition sign and place a subtraction sign.
#include using namespace std; </p>
int main()
{
int x;
int y;
cout<<“Have You Ever Wanted to subtract 2 Numbers? Of course! You can do that right here! Enter your first number!”;
cin>> x;
cout<<“Enter your second number!”;
cin>> y;
cout<<“The difference of the two numbers is ” << x-y;
system(“pause”);
return 0;
}</td> </tr> </tbody> </table>
Not hard at all. In fact, multiplication and division are practically the EXACT SAME. Just change the sign. Look below for an example of each.
MULTIPLICATION
#include using namespace std; </p>
int main()
{
int x;
int y;
cout<<“Have You Ever Wanted to Multiply 2 Numbers? Of course! You can do that right here! Enter your first number!”;
cin>> x;
cout<<“Enter your second number!”;
cin>> y;
cout<<“The product of the two numbers is ” << x*y;
system(“pause”);
return 0;
}</td> </tr> </tbody> </table>
DIVISION
#include using namespace std; </p>
int main()
{
int x;
int y;
cout<<“Have You Ever Wanted to Divide 2 Numbers? Of course! You can do that right here! Enter your dividend number!”;
cin>> x;
cout<<“Enter your divisor number!”;
cin>> y;
cout<<“The quotient of the two numbers is ” << x/y;
system(“pause”);
return 0;
}</td> </tr> </tbody> </table>
THE CALCULATOR
Now I think you’re ready to see what happens when everything is laced together. Our good friend iPodnerd wrote the following program for a class he taught and was gracious enough to let us use the code. Here is the following code for a simple calculator.
#include using namespace std; </p>
int main() { int choice; float first; float second; char quit = ‘n’; cout << “Welcome to The Calculator” << endl; while(quit == ‘n’) { cout << endl << “Type:\n” “‘1’ If you would like to add two numbers together\n” “‘2’ If you would like to subtract one number from another\n” “‘3’ If you would like to multiply two numbers together\n” “‘4’ If you would like to divide one number by another” << endl; cin >> choice;
if(choice == 1) { cout << “\nType the first number in the equation: “; cin >> first; cout << endl << “Type the second number in the equation: “; cin >> second; cout << “The sum of ” << first << ” and ” << second << ” is ” << first+second; cout << “, do you want to quit? (y/n): “; cin >> quit; } if(choice == 2) { cout << “\nType the first number in the equation (the one to be subtracted from): “; cin >> first; cout << endl << “Type the second number in the equation (the one to subtract): “; cin >> second; cout << “The difference of ” << first << ” and ” << second << ” is ” << first-second; cout << “, do you want to quit? (y/n): “; cin >> quit; } if(choice == 3) { cout << “\nType the first number in the equation: “; cin >> first; cout << endl << “Type the second number in the equation : “; cin >> second; cout << “The product of ” << first << ” and ” << second << ” is ” << first*second; cout << “, do you want to quit? (y/n): “; cin >> quit; } if(choice == 4) { cout << “\nType the first number in the equation (the dividend): “; cin >> first; cout << endl << “Type the second number in the equation (the divisor): “; cin >> second; cout << “The quotient of ” << first << ” and ” << second << ” is ” << first/second; cout << “, do you want to quit? (y/n): “; cin >> quit; } }
system(“pause”) return 0; }</td> </tr> </tbody> </table>
This may have come across as intimidating but it uses simple functions. Look at it carefully and notice the following two points as you scan the code:
-
At the beginning of the entire program, everything look standard, right? iPodnerd defined all of the variables and all that happiness. But he used floats instead of integers! What the !@#$ is a float, you ask. Well, a float is another way of looking at integers. If you have trouble understanding floats, refer to this tutorial.
-
The rest of the code is just like the addition, subtraction, multiplication, and division segments, but all in one. He reuses the same variable to make the code more concise and easier to follow.
That wasn’t so hard was it? Of course not! Next time, we’ll show you other C++ Math functions. And remember the following:
-
Addition uses “+” when adding numbers
-
Subtraction uses “-” when subtracting numbers
-
Multiplication uses “*” when multiplying numbers
-
Division uses “/” when dividing numbers
Happy trails!
| | | | | |
Leave a Comment