C++ Basic Exercises: Practice & Solutions
This article provides a collection of basic C++ exercises designed to help beginners solidify their understanding of fundamental concepts. Each exercise comes with a detailed solution, allowing you to compare your approach and learn from different strategies. Let's dive in!
Exercise 1: Hello World!
Objective: Print "Hello World!" to the console.
Solution:
#include
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
Explanation:
- #include <iostream>: This line includes the input/output stream library, which allows us to interact with the console.
- int main() {}: This is the main function, the entry point of every C++ program.
- std::cout << "Hello World!" << std::endl;: This line prints the text "Hello World!" to the console using the
std::cout
object and ends the line withstd::endl
.
Exercise 2: Variables and Data Types
Objective: Declare variables of different data types and print their values.
Solution:
#include
int main() {
int age = 25;
double height = 1.75;
char initial = 'A';
std::string name = "John Doe";
std::cout << "Age: " << age << std::endl;
std::cout << "Height: " << height << std::endl;
std::cout << "Initial: " << initial << std::endl;
std::cout << "Name: " << name << std::endl;
return 0;
}
Explanation:
- int age = 25;: Declares an integer variable named
age
and assigns the value 25 to it. - double height = 1.75;: Declares a double-precision floating-point variable named
height
and assigns the value 1.75 to it. - char initial = 'A';: Declares a character variable named
initial
and assigns the character 'A' to it. - std::string name = "John Doe";: Declares a string variable named
name
and assigns the string "John Doe" to it.
Exercise 3: Basic Arithmetic Operations
Objective: Perform basic arithmetic operations (addition, subtraction, multiplication, division) with two integers.
Solution:
#include
int main() {
int num1 = 10;
int num2 = 5;
int sum = num1 + num2;
int difference = num1 - num2;
int product = num1 * num2;
double quotient = (double) num1 / num2; // Note: Cast to double for floating-point division
std::cout << "Sum: " << sum << std::endl;
std::cout << "Difference: " << difference << std::endl;
std::cout << "Product: " << product << std::endl;
std::cout << "Quotient: " << quotient << std::endl;
return 0;
}
Explanation:
- int num1 = 10, num2 = 5;: Declares two integer variables and initializes them.
- int sum = num1 + num2; ... double quotient = (double) num1 / num2; Performs the arithmetic operations and stores the results in respective variables.
- (double) num1 / num2: The cast to
(double)
ensures a floating-point division even if both operands are integers.
Exercise 4: Conditional Statements
Objective: Write a program that checks if a given number is even or odd.
Solution:
#include
int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;
if (number % 2 == 0) {
std::cout << number << " is even." << std::endl;
} else {
std::cout << number << " is odd." << std::endl;
}
return 0;
}
Explanation:
- std::cin >> number;: Reads the input from the console and stores it in the
number
variable. - if (number % 2 == 0) { ... } else { ... }: This is an
if-else
statement that checks if the remainder ofnumber
divided by 2 is equal to 0. If it is, the number is even; otherwise, it's odd.
Exercise 5: Loops
Objective: Write a program that prints the first 10 natural numbers.
Solution:
#include
int main() {
for (int i = 1; i <= 10; i++) {
std::cout << i << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- for (int i = 1; i <= 10; i++) { ... }: This is a
for
loop, which iterates 10 times. The loop variablei
is initialized to 1, and in each iteration, it is incremented by 1 until it reaches 10. Inside the loop, the current value ofi
is printed.
Conclusion
These basic exercises provide a foundation for learning C++. By understanding these concepts and practicing consistently, you can build upon them to tackle more complex problems in the future. Remember to experiment with different variations of these exercises and explore other fundamental concepts in C++ to enhance your programming skills.