Practicing exercises is one of the best ways to master C++ programming. Here, we provide a collection of exercises ranging from beginner to advanced levels. These exercises will help you build a solid foundation in C++ concepts and improve your problem-solving skills.
Beginner-Level Exercises
1. Hello World
Write a program that prints “Hello, World!” to the console.
Challenge: Modify the program to print “Hello, [Your Name]!”
2. Sum of Two Numbers
Write a program that takes two numbers as input and outputs their sum.
Example Input:
5
10
Example Output:
Sum: 15
3. Odd or Even
Write a program that checks if a given number is odd or even.
Example Input:
4
Example Output:
Even
4. Find the Largest of Three Numbers
Write a program that takes three numbers as input and determines the largest.
Example Input:
3, 7, 5
Example Output:
Largest: 7
5. Simple Calculator
Write a program that performs addition, subtraction, multiplication, and division based on user input.
Example Input:
Enter two numbers: 12 4
Choose operation (+, -, *, /): *
Example Output:
Result: 48
Intermediate-Level Exercises
6. Factorial of a Number
Write a program to calculate the factorial of a number using a loop.
Example Input:
5
Example Output:
Factorial: 120
7. Fibonacci Sequence
Write a program to print the first n
numbers in the Fibonacci sequence.
Example Input:
Enter n: 5
Example Output:
0 1 1 2 3
8. Prime Number Checker
Write a program that checks if a number is prime.
Example Input:
7
Example Output:
Prime
9. Reverse a String
Write a program that reverses a given string.
Example Input:
hello
Example Output:
olleh
10. Find the Largest Element in an Array
Write a program to find the largest element in a given array.
Example Input:
{3, 5, 7, 2, 8}
Example Output:
Largest: 8
Advanced-Level Exercises
11. Matrix Multiplication
Write a program to multiply two matrices.
Example Input:
Matrix A: [[1, 2], [3, 4]]
Matrix B: [[5, 6], [7, 8]]
Example Output:
Resultant Matrix: [[19, 22], [43, 50]]
12. Binary Search
Implement a binary search algorithm to find an element in a sorted array.
Example Input:
Array: {1, 3, 5, 7, 9}
Element to find: 7
Example Output:
Element found at index 3
13. Palindrome Checker
Write a program to check if a string is a palindrome.
Example Input:
madam
Example Output:
Palindrome
14. File Operations
Write a program to create a file, write data into it, and read the data back.
15. Custom Sorting
Write a program to sort an array of strings alphabetically.
Example Input:
{"apple", "banana", "cherry"}
Example Output:
{"apple", "banana", "cherry"}
16. Student Grade System
Create a program to manage student records and calculate grades based on their marks.
17. Simple Encryption
Write a program that encrypts a string using a simple Caesar cipher.
Example Input:
String: hello
Shift: 3
Example Output:
Encrypted: khoor
18. Tic-Tac-Toe Game
Create a program that allows two players to play Tic-Tac-Toe.
19. Find Duplicates in an Array
Write a program to find duplicate elements in an array.
Example Input:
{1, 2, 3, 2, 5}
Example Output:
Duplicates: 2
20. Simulate a Bank ATM
Write a program to simulate an ATM system with features like balance inquiry, deposit, and withdrawal.
Tips for Solving Exercises
- Understand the Problem
Read the problem statement carefully before writing code. - Plan the Solution
Break the problem into smaller steps and solve them systematically. - Test Your Code
Test your solution with multiple inputs, including edge cases. - Use Comments
Add comments to your code for better readability.
Practice with The Coding College 🚀
For more hands-on exercises and solutions, visit The Coding College. Our platform offers step-by-step tutorials and challenges designed to enhance your programming skills in C++. Happy coding!