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
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
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
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
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
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
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
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
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
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
Output
Click Run Button to view compiled output