CPSC 1050 – Practice Set 6
C++
1. (Lists) Write a C++ program that creates a list with 4 elements of your choice and displays the first and last
elements of the list.
2. (Temperature Conversion) Write a C++ program that reads the temperature in Fahrenheit, converts it to
Celsius, and displays the result.
3. (Area of a rectangle) Write a C++ program that reads two values for height and width of a rectangle. If both
values are positive, the program calculates and displays the area of the rectangle. If any of the numbers is
negative, the program displays a warning message. Here are sample runs:
Enter height and width: 4 6
Area of the rectangle is 24.
Enter height and width: -4 6
The values must be positive.
4. (Calculations) Write a pseudocode and a C++ program that prompts the user to enter two numbers and
checks whether the first number is divisible by the second. [Hint: Use modulus (%)]. Here is a sample run:
Enter two numbers: 24 3
24 is divisible by 3
5. (Logical Operators) Write a C++ program that prompts the user to enter an integer and determines whether it
is divisible by 5 and 6, whether it is divisible by 5 or 6, and whether it is divisible by 5 or 6, but not both. [Hint:
Use logical operators (and, or, not)]
6. (Odd or Even) Write a C++ program that prompts the user to enter a number and determines if it is even or
odd.
7. (Even and Odd Numbers) Write a C++ program that prints the even numbers from 2 to 10 in one row. The
program then prints the odd numbers from 9 to 1 in one row. Is there any input to this program?
8. Write a C++ program that reads 10 numbers from the input within a loop. The program calculates the sum of
all the positive numbers, so if the number is positive, add it to sum and if the number is negative, print "_____
is negative" replacing the underline with the value of the number.
9. (Count positive and negative numbers) Write a C++ program that reads an unspecified number of integers,
determines how many positive and negative values have been read, and computes the total and average of
the positive values (not counting zero and negative values). Your program ends with the input 0. Display the
average as a floating-point number.
10. (Factorials) The factorial of a positive integer n (written n! and pronounced "n factorial") is equal to the
product of the positive integers from 1 to n. Write a C++ program that receives a positive number and prints
the factorial of the number. Use your program to print the factorials of 1 to 10.
1
11. (Simple Calculator) Write a C++ program that asks the user to enter two integers and an arithmetic operator
(+, -, *, /). Then based on the operator calculate the result. If an invalid operator is entered, give a message to
the user. Notice that the result for division is float. Here are sample runs:
Enter two integers: 4 8
Enter an arithmetic operator (+, -, *, /): +
Result = 12
Enter two integers: 4 8
Enter an arithmetic operator (+, -, *, /): /
Result = 0.5
Enter two integers: 4 8
Enter an arithmetic operator (+, -, *, /): $
Invalid operator.