30 C++ Basic Exercises for Beginners with Solutions

Master beginner 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 intermediate challenges. Start your journey to C++ mastery today!

Learning Objectives:

By the end of these exercises, you will have a strong understanding of C++ basics, including variables, control structures, functions, and basic input/output operations.

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 print "Hello, World!".

Required Input:

Expected Output:

Hello, World!

Code In Cpp

#include int main() { // Logic to print Hello, World! return 0; }

Output

Click Run Button to view compiled output

2. Create a program that adds two integers and displays the result.

Required Input:

a = 5, b = 10

Expected Output:

Sum = 15

Code In Cpp

#include int main() { int a = 5, b = 10; // Logic to add a and b return 0; }

Output

Click Run Button to view compiled output

3. Write a program to find the maximum of three numbers.

Required Input:

a = 3, b = 7, c = 5

Expected Output:

Maximum = 7

Code In Cpp

#include int main() { int a = 3, b = 7, c = 5; // Logic to find the maximum return 0; }

Output

Click Run Button to view compiled output

4. Implement a program to calculate the factorial of a number.

Required Input:

n = 5

Expected Output:

Factorial = 120

Code In Cpp

#include int main() { int n = 5; // Logic to calculate factorial return 0; }

Output

Click Run Button to view compiled output

5. Write a program to check if a number is even or odd.

Required Input:

n = 4

Expected Output:

4 is Even

Code In Cpp

#include int main() { int n = 4; // Logic to check even or odd return 0; }

Output

Click Run Button to view compiled output

6. Create a program to swap two numbers using a temporary variable.

Required Input:

a = 5, b = 10

Expected Output:

After swapping: a = 10, b = 5

Code In Cpp

#include int main() { int a = 5, b = 10; // Logic to swap a and b return 0; }

Output

Click Run Button to view compiled output

7. Write a program to calculate the sum of digits of a number.

Required Input:

n = 123

Expected Output:

Sum of digits = 6

Code In Cpp

#include int main() { int n = 123; // Logic to calculate sum of digits return 0; }

Output

Click Run Button to view compiled output

8. Implement a program to reverse a number.

Required Input:

n = 12345

Expected Output:

Reversed Number = 54321

Code In Cpp

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

Output

Click Run Button to view compiled output

9. Create a program to find the length of a string.

Required Input:

str = "Hello"

Expected Output:

Length = 5

Code In Cpp

#include #include int main() { std::string str = "Hello"; // Logic to find the length of the string return 0; }

Output

Click Run Button to view compiled output

10. Write a program to concatenate two strings.

Required Input:

str1 = "Hello", str2 = " World"

Expected Output:

Concatenated String = Hello World

Code In Cpp

#include #include int main() { std::string str1 = "Hello"; std::string str2 = " World"; // Logic to concatenate strings return 0; }

Output

Click Run Button to view compiled output

ad vertical

1 of 3