Problems

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

  1. Take length and breadth of a rectangle and check if it is a square or not.
  2. Take age as user input from 3 users. Find the youngest and oldest among them.
  3. Ask user for a year and find if it is a leap year or not.
  4. 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 a if-else if chain for conversion based on the unit. Handle invalid unit input gracefully.
  5. 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

    1. 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 2024. 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.
      * 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 else if statements to achieve the result
      Salary 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”

    7. Write a C++ program to check if a student is eligible for admission based on their exam scores. To be eligible, the student must:

    • Have a math score of 70 or above
    • Have a science score of 65 or above
    • Have an overall average score of 75 or above

     

    8. Write a C++ program that determines if a person qualifies for a loan. The criteria for loan approval are:

    • The person must be at least 21 years old.
    • Their credit score must be 700 or higher.
    • They must either have a stable job (isEmployed = true) or own a property (ownsProperty = true).

    9. Write a C++ program to check if a person is eligible to vote in an election. The eligibility criteria are:

    • The person must be at least 18 years old.
    • The person must be a citizen.
    • The person must not have any active voting restrictions (hasRestrictions = false).

    10. Write a C++ program that issues a weather alert based on temperature and wind speed. An alert is issued if:

    • The temperature is below 0°C, or
    • The wind speed is above 30 km/h and the temperature is below 10°C.

    CHAPTER 3: LOOPS

    1. Take a number from a user. Display number -1 to -n using a loop.

    2. Take a number n from the user. Calculate the sum of all numbers from 1 to n. Eg: if user enters, 5, it should show 15 because 1+2+3+4+5 = 15.
    Similarly if user enters 7, it must show 28 because 1+2+3+4+5+6+7 = 28.
     
    3. Print the following pattern
    5 4 3 2 1
    4 3 2 1
    3 2 1
    2 1
    1

    4. Write a c++ program that prints all the numbers from 0 to 19(including them both) except 3 and 6.

    5. Take a number from the user and find factorial of that given number. This is how to calculate a factorial .
    5!(read as 5 factorial) = 5 X 4 X 3 X 2 X 1 = 120
    similarly factorial of 7 = 7! = 7 X 6 X 5 X 4 X 3 X 2 X 1 = 5040.
     
    6. Take a number n from the user. Print the squares of number starting with 1 until n.
    Perfect square is square of a number. In other words, when a number is multiplied by itself
    For example, if user enter 6, the output must be squares of numbers between 1 and 6 (including 6)<br>
    1 4 9 16 25 36
     
    7. Write a Python program which iterates the integers from 1 to 50. For multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
     
    8. Write a program to print multiplication table of a given number. Print it gracefully.
     
    9. Write a program to count the total number of digits in a number using a while loop.

    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

    *
    * *
    * * *
    * * * *
    * * * * *
    * * * *
    * * *
    * *
    *
     
    14. Write a program that takes a text from the user and finds the number of time a vowel has occurred in that text
    15. Write a C++ program that simulates rolling a six-sided die 100 times. Use a loop to simulate the rolling, and use the rand() function to generate random numbers between 1 and 6. At the end of the simulation, print how many times each face (1 through 6) appeared.

    16. Write a C++ program that generates a random number between 1 and 100 and asks the user to guess the number. The program should give hints like “Too High” or “Too Low” after each guess. Use a loop to allow multiple guesses until the user correctly guesses the number or reaches a maximum of 10 attempts.

     

    Example Output:

    • Face 1 appeared 17 times
    • Face 2 appeared 18 times
    • Face 3 appeared 16 times
    • Face 4 appeared 20 times
    • Face 5 appeared 14 times
    • Face 6 appeared 15 times

    17. Write a C++ program that simulates a simple bank account. The program should ask the user for an initial balance and then allow them to perform transactions (deposit or withdraw) in a loop. The loop should continue until the user chooses to exit. Ensure that the user cannot withdraw more money than their current balance.

    • Use the following options in the loop:
      • Option 1: Deposit
      • Option 2: Withdraw
      • Option 3: Display Balance
      • Option 4: Exit

    18. Write a C++ program that simulates flipping a coin 50 times. Use a loop to simulate each coin flip, and randomly generate either “Heads” or “Tails” for each flip using rand(). At the end of the program, print how many times “Heads” and “Tails” were flipped.

    Example Output:

    • Heads: 28 times
    • Tails: 22 times

    19. Write a C++ program that simulates the test scores of 30 students. The program should generate random scores between 0 and 100 for each student. Then, the program should calculate and display the average score, the highest score, and the lowest score.

    Example Output:

    • Average score: 75.5
    • Highest score: 98
    • Lowest score: 43

    20. Write a C++ program that simulates logging daily temperatures for 7 days. The temperature for each day should be generated randomly between -10°C and 35°C. After logging the temperatures, the program should print the average temperature for the week and the number of days with below-freezing temperatures.

    Example Output:

    • Average temperature: 18°C
    • Number of days with freezing temperatures: 2

    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:

    1. You have two arrays: one for the product names and one for the corresponding prices.
    2. Display the list of products to the user.
    3. Allow the user to select a product by entering its index (starting from 1).
    4. Ask the user to enter the quantity of the selected product.
    5. Calculate the total price for that product (price * quantity).
    6. Display the total cost to the user.