
C++ Programming Exercises
Summary
Try to work through the exercises on each lesson before moving onto the next. You may find the
following web resource helpful:
•http://www.cplusplus.com
Provides tutorials, a user’s forum and documentation on the most widely used libraries including
strings, I/O and the STL.
Lesson 1 - Hello World
1. Find out what is the difference between a “signed” and an “unsigned” integer. When might you
use an unsigned integer instead of a signed integer?
2. You have seen the “fundamental data type” called int but you will eventually need to use others.
Amend the program to create a char data type, a bool data type and a double data type. Give
them appropriate names and values and print their values to screen using cout.
3. Amend the code so that it asks for two numbers from the user. Store these numbers in separate
integer variables and then print to screen their sum and product on separate lines. Use cin and
cout—as well as the new line charactor—to accomplish this task.
4. Amend the code from Lesson 1 so that the program asks for your first name and then prints
“Hello (your name)!” to the screen. Use the cin and cout classes to accomplish this. In
addition, use the string variable type in the std namespace to store your name.
Lesson 2 - Functions
1. Not every function we may write has to return a value. Write a function called product which
accepts two integers as arguments (like add and minus) but returns no value. In the function
body the product function should use cout to print the product directly to screen.
2. Write a function called quotient which should take a double argument and an integer argument.
This function should also return a double data type. Now if you provide the quotient function
with the values 5 and 3 say, it should return the value 1.66667 or there-abouts. Test this by
calling the function within a cout statement. Once this works change the double argument to
an integer and notice the result; why does quotient no longer return a double?
3. Write a “calculator program” which asks for two numbers and a mathematical operator (repre-
sented as a char type. Depending on the operator (+, -, * or /) call the appropriate function
and display the result. Use a switch statement to filter the choices.
Lesson 3 - Program Flow
1. Write a function that accepts an integer argument and returns a boolean value. The function
should return true if the integer argument is greater than or equal to zero, and false otherwise.
Call this function with a positive and negative value to test that it works, using cout to display
the result.
1