Computer Science and Programming
Prof. Gregory Safko

Lab 2


Arithmetic revisited (arith2.cpp)

Revise your “Arithmetic” program from Lab 1 so that it does not compute or display the divisions and remainder (mod, the % operator) when the second integer entered is zero.

Here is one possible output format, with several example runs:

Please enter an integer: 7
Please enter another integer: 8
7 + 8 is 15
7 - 8 is -1
7 * 8 is 56
7 / 8 (integer division) is 0
7 % 8 is 7
7 / 8 (floating-point division) is 0.875
Press any key to continue
 

 



 

 

 

Please enter an integer: 20
Please enter another integer: 0
20 + 0 is 20
20 - 0 is 20
20 * 0 is 0
Press any key to continue

2. Divisibility testing (divtest.cpp)

Write a program that prompts for an integer, and indicates whether it is even or odd, whether or not it is divisible by 3, and whether or not it is divisible by 4. (Hint: use the % operator. "Even" means "divisible by 2".)

Here is one possible output format, showing two example runs:

Enter an integer: 8
8 is even.
8 is not divisible by 3.
8 is divisible by 4.
Press any key to continue
 

 



 

 

 

Enter an integer: 9
9 is odd.
9 is divisible by 3.
9 is not divisible by 4.
Press any key to continue
 

 



 

 

 

Enter an integer: 10
10 is even.
10 is not divisible by 3.
10 is not divisible by 4.
Press any key to continue

3. Divisibility testing, with a loop (divloop.cpp)

Make another copy of your divisibility-testing program, and use a loop to allow the user to test as many integers as they want.
Use any suitable mechanism to let the user quit: for example, stopping when 0 is entered, or else asking whether to continue at the end of every iteration. (Make sure the user is told how to do it!)


4. Printing a character in a loop (charloop.cpp)

Write a program that asks the user for a symbol (using a variable of type char, rather than of type int or double) and a number of copies to print, and prints a line of that many copies of that symbol.

(If the user enters an integer less than 1 for the number of copies to print, a properly written loop will iterate zero times, and not print out any symbols. For this lab problem, this behavior is fine; there is no need for additional error checking.)

Here is one possible output format:

What symbol would you like to print? b
How many copies? 6
bbbbbb
Press any key to continue
 


5. Counting up and down (countUpDown.cpp)

Write a program that asks the user for an integer, and counts up from 1 to that integer and then back down, with all the numbers on the same line.

The output should look exactly like mine, given the same input. Here are two example runs:

Enter an integer: 6
1 2 3 4 5 6 5 4 3 2 1 
Press any key to continue
 

 



 

 

 

Enter an integer: 1
1 
Press any key to continue

Hints: