Your one stop guide to polish your C++ skills.
Green means easy. Orange means challenging. Red is a bit difficult. The difficulty of these questions are with reference to the course taught in class and takes into account the average performance of students in assignments and quizzes. Some might feel the reds == easy for them while some students might feel orange == difficult.
CHAPTER 1: Introduction(Variables, Operators, escape sequence)
- Enter an input from user asking their age and name. Prompt the user to enter their name and age. Store the input in appropriate variables(string and int). Print a greeting that includes the entered name and age.
- Take three integer input from the user. These input are the scores they received in a test(out of 20). Print the total scores, average and names of the subjects in a single line using three different print statements. Hint: How do you stitch multiple prints?
- Take age from the user and save it in a variable. Print the number of years required for that user to reach the age of 50.
- Take two numbers from the user. Perform an operation involving two arithmetic operations. It can be any arithmetic operation.
- Ask the user to enter the length and width of a rectangle. Use
float
variables to handle potential decimal values. Calculate and print the area of the rectangle.
CHAPTER 2: CONDITIONS
- Take length and breadth of a rectangle and check if it is a square or not.
- Take age as user input from 3 users. Find the youngest and oldest among them.
- Ask user for a year and find if it is a leap year or not.
- Allow the user to enter a temperature value(between two values). Ask whether the value is in Celsius (C) or Fahrenheit (F). Use a
char
variable (unit
) to store the user’s choice. Implement aif-else if
chain for conversion based on the unit. Handle invalid unit input gracefully.Write a program to accept cost price of a car and display road tax to be paid according to following table.- Price of the car and equivalent road tax to be paid on brand-new cars
Price of the car Road tax to be paid < $50,000 5% $50,000 – $99,999 10% $100,000 – 149,999 15% >150,000 20%
- Price of the car and equivalent road tax to be paid on brand-new cars
- Hey! It’s that time of the year. Warm coffee, Gloomy weather, and filling taxes…..wait?WHAT!!
In this exercise, you will not only learn to condition but also to calculate taxes.Imagine user being a single filer for 2023. The tax rate by IRS can be found here. In total, there are 7 tax brackets, we will deal with 5 of them as shown in table below.<br>* Take an input salary from the user.* Based on the salary, calculate the tax amount the user owns to the IRS.
Use a series of if elif statements to achieve the resultSalary Tax < $11,000 10% $11,000 – $44,725 $1,100 + 12% of difference $44,725 – $95,375 $5,147 + 22% of the difference $95,375 – $182,100 $16,290 + 24% of the difference $182,100 – $231,250 $37,104 + 32% of the difference >$231,250 Contact our tax experts
6. Ask user for their age, gender(M or F), and marital status(Y or N). Based on the following table, print the appropriate message.
- If gender is female, “urban areas”
- If gender is male and age is between 20 and 40, “anywhere”
- If gender is male and age is between 40 and 60, “urban areas”
- Other input, “Error”
CHAPTER 3: LOOPS
1. Take a number from a user. Display number -1 to -n using a loop.
4. Write a c++ program that prints all the numbers from 0 to 19(including them both) except 3 and 6.
For example, the number is 75869, so the output should be 5.
10. Write a program to display all prime numbers within a range
Note: A Prime Number is a number that cannot be made by multiplying other whole numbers. A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers
Examples:
- 12 is not a prime number because it can be made by 3×4 = 12
- 37 is a prime number because no other whole numbers multiply together to make it.
11. Write a program to print the cube of all numbers from 1 to a given number.
If input is 6 print 1 8 27 64 125 216
12. Take a number from the user and reverse that integer number
Given: 587249
Expected output: 942785
13. Print the following pattern
CHAPTER 4: FUNCTION and FUNCTION OVERLOADING
1. Consider a class Calculator in a programming language that supports function overloading. Implement function overloading by defining three methods named add within the Calculator class. Each method should calculate the sum but must differ in the number or type of its parameters:
- The first add method should take two integers and return their sum.
- The second add method should take three integers and return their sum.
- The third add method should take two double values and return their sum.
2. You are developing a software for a library to help manage book checkouts. Write a function in C++ named checkoutBook
that simulates the checkout process. This function should take three parameters:
bookTitle
(string) – the title of the book being checked out.days
(int) – the number of days the book is being checked out for.memberID
(int) – the library membership ID of the user checking out the book.
CHAPTER 5: Pass by value vs Pass by reference
1. Write the function definition to calculate the total cost for a trip. Keep the main function as is and follow the given function prototype for the definition that will be below the main function.
The function will:
- take in the number of people traveling (by value)
- take in the number of nights for the trip (by value)
- take in the trip cost (by reference)
- calculate the trip cost
- air tickets are 300 per person
- hotel is 150 for each night of the trip
- trip cost is the sum of the air ticket and hotel costs
-
DOES NOT return a value
Sample input:
2 // number of people
3 // number of nights
Sample output:
1050 // sum of 600 (2 air tickets) and 450 (3 hotel nights)
2. You are tasked with creating a function that modifies two variables passed to it by a user. Write a C++ program that includes a function named swapNumbers
which takes two integer variables as arguments and swaps their values. This function should demonstrate the use of passing parameters by reference. Also, include a main
function where you define two integer variables, call swapNumbers
using these variables, and print their values before and after the call to swapNumbers
to show that their values have been swapped.
Chapter 6: Arrays
1. Write a program that calculate the sum of all elements in an integer array
2. Write a program to find and print the maximum value in array of integers.
3. Extend program 1 to calculate the average of all element in an array. Take n = number of elements from the user as well as the integers inside the array.
4. Create a program that counts the number of even and odd number.
5. Write a C++ program that allows users to select products from a list and enter the quantity they wish to purchase. The program will then calculate the total cost of the products based on the prices stored in an array.
Steps:
- You have two arrays: one for the product names and one for the corresponding prices.
- Display the list of products to the user.
- Allow the user to select a product by entering its index (starting from 1).
- Ask the user to enter the quantity of the selected product.
- Calculate the total price for that product (price * quantity).
- Display the total cost to the user.