MidtermPracticeProblems (1)
MidtermPracticeProblems (1)
Q1. Calculates a customer’s bill for a local cable company. There are two types of customers: residential
and business. There are two rates for calculating a cable bill: one for residential customers and one for
business customers. For residential customers, the following rates apply:
The program should ask the user for an account number (an integer) and a customer code. Assume that
R or r stands for a residential customer, and B or b stands for a business customer
Input:
The customer’s account number, customer code, number of premium channels to which the user
subscribes, and, in the case of business customers, number of basic service connections.
Output:
Customer’s account number and the billing amount
Q2. Write a program that calculates and prints the bill for a cellular telephone company.
The company offers two types of service: regular and premium. Its rates vary, depending on the type of
service. The rates are computed as follows:
Regular Service: $10.00 plus first 50 minutes are free. Charges for over 50 minutes are $0.20 per
minute.
a. For calls made from 6:00 a.m. to 6:00 p.m., the first 75 minutes are free; charges for
more than 75 minutes are $0.10 per minute.
b. For calls made from 6:00 p.m. to 6:00 a.m., the first 100 minutes are free; charges for
more than 100 minutes are $0.05 per minute.
Your program should prompt the user to enter an account number, a service code (type char), and the
number of minutes the service was used. A service code of r or R means regular service; a service code
of p or P means premium service. Treat any other character as an error. Your program should output the
account number, type of service, number of minutes the telephone service was used, and the amount
due from the user.
For the premium service, the customer may be using the service during the day and the night.
Therefore, to calculate the bill, you must ask the user to input the number of minutes the service was
used during the day and the number of minutes the service was used during the night.
Q3. Write a C++ program that takes 20 integers as input from the user and then display the frequency of
even, odd and zero numbers.
Input:
20 integers
Output:
The number of zeros, even numbers and zeros
Sample Run
Input: 0 0 -2 -3 -5 6 7 8 0 3 0 -23 -8 0 2 9 0 12 67 54 0 0 -2 -3 -5 6 7 8 0 3 0 -23 -8 0 2 9 0 12 67 54
Output:
There are 13 evens, which includes 6 zeros.
The number of odd numbers is: 7
Q4. Write a program that prompts the user to input an integer and then outputs both the individual
digits of the number and the sum of the digits. For example, it should output the individual digits of
3456 as 3 4 5 6, output the individual digits of 8030 as 8 0 3 0, output the individual digits of 2345526 as
2 3 4 5 5 2 6, output the individual digits of 4000 as 4 0 0 0, and output the individual digits of -2345 as 2
3 4 5.
Q5. Write a program that uses while loops to perform the following steps:
a. Prompt the user to input two integers: firstNum and secondNum (firstNum must be less than
secondNum).
b. Output all odd numbers between firstNum and secondNum.
c. Output the sum of all even numbers between firstNum and secondNum.
d. Output the numbers and their squares between 1 and 10.
e. Output the sum of the square of the odd numbers between firstNum and secondNum.
f. Output all uppercase letters (Hint: increment ASCII Code)
Q6. The population of a town A is less than the population of town B. However, the population of town A
is growing faster than the population of town B. Write a program that prompts the user to enter the
population and growth rate of each town. The program outputs after how many years the population of
town A will be greater than or equal to the population of town B and the populations of both the towns
at that time. (A sample input is: Population of town A = 5000, growth rate of town A = 4%, population of
town B = 8000, and growth rate of town B = 2%.)
Q7. (Apartment problem) A real estate office handles, say, 50 apartment units. When the rent is, say,
$600 per month, all the units are occupied. However, for each, say, $40 increase in rent, one unit
becomes vacant. Moreover, each occupied unit requires an average of $27 per month for maintenance.
How many units should be rented to maximize the profit?
1. Write a C++ Program to initialize two 3x3 matrices (A & B) and then store their sum in matrix C.
Display sum on the console screen as well. Hint: use 2D array for storing A, B and C.
Q9. Attempt the following by writing C++ code and using conditional operators determine:
Whether the character entered through the keyboard is a lower-case alphabet or not.
Whether a character entered through the keyboard is a special symbol or not.
Write a program using conditional operators to determine whether a year entered through the
keyboard is a leap year or not.
Q10. A shape with four vertices and sides is known as a quadrilateral. Square, rectangle, kite, rhombus,
trapezium, and parallelogram are all quadrilaterals. Write a C++ program for guessing the type of the
quadrilateral. Your program should guess correctly, in case of incorrect input, display appropriate
message. Phrase all questions in a way that user must enter some boolean value Y/N as an answer. Both
Y/y are treated as ‘Yes’.
Example: Think of a quadrilateral from square, rectangle, kite, rhombus, trapezium and
parallelogram and I will guess it....
Let’s start [Optional: you can place counter here like display 3, 2, 1....]
Q1. Are all sides equal? (Y or N) Y
Q2. Are all angles 90 degrees? (Y or N)
I got it! It’s a SQUARE.
Q11. You were orphaned at an early age. To make ends meet, you sought work in a local toy car
workshop in your city, and your job is to build toy cars from a collection of parts. Each toy car needs 4
wheels, 1 car body, and 2 figures of people to be placed inside. You notice that sometimes, you’ll get
orders of the number of cars to make, but you won’t have the total required parts available. To prevent
this, you want to find out how many complete toy cars can you make, given the total number of wheels,
car bodies and figures available at hand currently.
Q12. Write a C++ program that gives the largest number using ternary operator among:
Three Numbers that is if three numbers are taken as input form user.
Four Numbers that is if four numbers are taken as input form user.
Q13. Write a program that displays a full pyramid and an inverted pyramid using *