C++ Assignment 2
Write a C++ program that prints the sum, difference, product, quotient and remainder of 60 and 7.
The main
function should have 5 cout
statements. The first one is shown below:
cout<<"60 + 7 ="<<60 + 7<<endl;
Remember to
- include sample output with your program.
- Make sure your name, class and assignment# are in comments at the top of the program
- Attach your completed code (the
.cpp
file) to an email and send it to mrsimon@lycos.com (and email a backup copy to yourself!)
- Make sure your name, class and assignment# are also on the subject line of the email
Background to Assignment #2
C++ uses the following operators for arithmetic: + for add, - for subtract, * for multiply, / for divide
and % for modulus (remainder of integer division).
cout<<2 + 3<<endl;
// displays the number 5 to the screen
Division
While addition, subtraction and multiplication are straightforward, division in C++ works differently
depending on whether you are using integers or decimals. Try the following two lines of code and see
if you can explain why the two lines display different numbers.
cout<<7/5<<endl;
cout<<7.0/5<<endl;
Modulus
C++ treats division with integers differently than division with decimals. You may remember in grade school
that when you divide 5 by 2, the answer is 2 with a remainder of 1. You can get the remainder by using
the modulus operator:
cout<<7%5<<endl;
Expressions and Literals
Putting double quotes around a group of numbers changes it from an expression to a literal.
See if you can explain why the following two lines display different things to the screen:
cout<<5 + 2<<endl;
cout<<"5 + 2"<<endl;