30 C++ Basic Exercises for Intermediate with Solutions

Master intermediate C++ skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in C++, setting a solid foundation for advanced challenges. Start your journey to C++ mastery today!

Learning Objectives:

By the end of these exercises, you will be skilled in intermediate C++ concepts, such as classes, inheritance, pointers, and dynamic memory management.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Write a program to find the GCD (Greatest Common Divisor) of two numbers using the Euclidean algorithm.

Required Input:

int a = 56, b = 98;

Expected Output:

GCD = 14

Code In Cpp

#include int main() { int a = 56, b = 98; // Logic to find GCD return 0; }

Output

Click Run Button to view compiled output

2. Implement a program to merge two sorted arrays into one sorted array.

Required Input:

int arr1[] = {1, 3, 5}, arr2[] = {2, 4, 6};

Expected Output:

Merged array: 1 2 3 4 5 6 

Code In Cpp

#include int main() { int arr1[] = {1, 3, 5}, arr2[] = {2, 4, 6}; // Logic to merge arrays return 0; }

Output

Click Run Button to view compiled output

3. Create a program that counts the number of words in a given string.

Required Input:

std::string str = "Count the number of words in this string";

Expected Output:

Word Count = 8

Code In Cpp

#include #include #include int main() { std::string str = "Count the number of words in this string"; // Logic to count words return 0; }

Output

Click Run Button to view compiled output

4. Write a program to check if a given number is a power of two.

Required Input:

int n = 16;

Expected Output:

16 is a power of 2.

Code In Cpp

#include int main() { int n = 16; // Logic to check power of two return 0; }

Output

Click Run Button to view compiled output

5. Create a program to implement binary search on a sorted array.

Required Input:

int arr[] = {2, 4, 6, 8, 10}; int target = 6;

Expected Output:

Element found at index: 2

Code In Cpp

#include int main() { int arr[] = {2, 4, 6, 8, 10}, target = 6; // Logic for binary search return 0; }

Output

Click Run Button to view compiled output

6. Write a program to find the second largest number in an array.

Required Input:

int arr[] = {3, 5, 1, 4, 2};

Expected Output:

Second largest number = 4

Code In Cpp

#include int main() { int arr[] = {3, 5, 1, 4, 2}; // Logic to find second largest number return 0; }

Output

Click Run Button to view compiled output

7. Create a program to reverse an integer without converting it to a string.

Required Input:

int n = 12345;

Expected Output:

Reversed number = 54321

Code In Cpp

#include int main() { int n = 12345; // Logic to reverse the integer return 0; }

Output

Click Run Button to view compiled output

8. Write a program to find the sum of the digits of a number recursively.

Required Input:

int n = 12345;

Expected Output:

Sum of digits = 15

Code In Cpp

#include int sumOfDigits(int n); int main() { int n = 12345; // Logic to find the sum of digits return 0; }

Output

Click Run Button to view compiled output

9. Implement a program to check if two strings are rotations of each other.

Required Input:

std::string str1 = "abcd", str2 = "cdab";

Expected Output:

Strings are rotations of each other.

Code In Cpp

#include #include int main() { std::string str1 = "abcd", str2 = "cdab"; // Logic to check for rotations return 0; }

Output

Click Run Button to view compiled output

10. Write a program to implement a simple text-based menu for user choices.

Required Input:

N/A (User interaction through the menu)

Expected Output:

Menu:
1. Option 1
2. Option 2
3. Option 3
4. Exit
Choose an option: Exiting...

Code In Cpp

#include int main() { // Logic for menu-driven program return 0; }

Output

Click Run Button to view compiled output

ad vertical

1 of 3