C++ Coding Exercises Worksheet
Instructions:
For each exercise, write the complete C++ program that solves the problem. Include
comments in your code to explain your thought process and the steps taken. Test
your code to ensure it works correctly before submission.
Exercise 1: Even or Odd
Write a program that prompts the user to enter an integer and determines whether
the number is even or odd. Output the result.
Sample Output:**
Enter an integer: 7
The number 7 is odd.
Exercise 2: Simple Calculator
Create a simple calculator program that performs addition, subtraction, multiplication,
and division based on user input. The program should prompt the user to enter two
numbers and an operator (+, -, *, /). Output the result.
Sample Output:
Enter first number: 10
Enter second number: 5
Enter operator (+, -, *, /): +
Result: 10 + 5 = 15
Exercise 3: Prime Number Checker
Write a program that checks whether a number entered by the user is a prime
number. A prime number is only divisible by 1 and itself.
Sample Output:
Enter a number: 11
11 is a prime number.
Exercise 4: Factorial Calculation
Create a program that calculates the factorial of a non-negative integer entered by
the user using a loop. Use a `for` loop to calculate the factorial.
Sample Output:
Enter a non-negative integer: 5
Factorial of 5 is 120.
Exercise 5: Grade Calculator
Design a program that calculates and displays a letter grade based on a percentage
entered by the user. Use the following standard grading scale:
⮚ A: 90-100
⮚ B: 80-89
⮚ C: 70-79
⮚ D: 60-69
⮚ F: 0-59
Sample Output:
Enter your percentage: 85
Your letter grade is B.
Exercise 6: Multiplication Table
Write a program that takes a number from the user and prints its multiplication table
(from 1 to 10).
Sample Output:
Enter a number: 3
Multiplication Table for 3:
3x1=3
3x2=6
3x3=9
3 x 10 = 30
Exercise 7: Sum of Digits
Create a program that calculates the sum of the digits of a positive integer entered by
the user.
Sample Output:
Enter a positive integer: 12345
The sum of the digits is 15.
Exercise 8: Reverse a Number
Write a program that reverses the digits of a number entered by the user.
Sample Output:
Enter a number: 12345
The reversed number is 54321.
Exercise 9: FizzBuzz
Implement the FizzBuzz problem. Your program should print the numbers from 1 to
100, but for multiples of 3, print "Fizz" instead of the number, and for the multiples of
5, print "Buzz." For numbers that are multiples of both three and five, print "FizzBuzz."
Sample Output:
Fizz
Buzz
Fizz
Fizz
Buzz
...
Exercise 10: Character Counting
Write a program that prompts the user to enter a string and counts the number of
vowels in that string.
Sample Output:
```
Enter a string: Hello World
The number of vowels in "Hello World" is 3.
```