0% found this document useful (0 votes)
5 views4 pages

Exercises

The document contains a worksheet with ten C++ coding exercises, each requiring the creation of a complete program to solve specific problems. Exercises include determining if a number is even or odd, creating a simple calculator, checking for prime numbers, calculating factorials, determining letter grades, generating multiplication tables, summing digits, reversing numbers, implementing FizzBuzz, and counting vowels in a string. Each exercise includes sample outputs to guide the implementation.

Uploaded by

Nadia Oweinat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Exercises

The document contains a worksheet with ten C++ coding exercises, each requiring the creation of a complete program to solve specific problems. Exercises include determining if a number is even or odd, creating a simple calculator, checking for prime numbers, calculating factorials, determining letter grades, generating multiplication tables, summing digits, reversing numbers, implementing FizzBuzz, and counting vowels in a string. Each exercise includes sample outputs to guide the implementation.

Uploaded by

Nadia Oweinat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

```

You might also like