Here is a simple C++ practice to help you get started with the language:
- Write a program that prompts the user to input two integers, and then prints the sum of these two integers.
cCopy code#include <iostream>
int main() {
int num1, num2, sum;
std::cout << "Enter two integers: ";
std::cin >> num1 >> num2;
sum = num1 + num2;
std::cout << "The sum of " << num1 << " and " << num2 << " is " << sum << std::endl;
return 0;
}
- Write a program that prompts the user to input a radius of a circle, and then calculates and prints the area and circumference of the circle.
cCopy code#include <iostream>
#include <cmath>
int main() {
double radius, area, circumference;
const double pi = 3.14159265358979323846;
std::cout << "Enter the radius of the circle: ";
std::cin >> radius;
area = pi * pow(radius, 2);
circumference = 2 * pi * radius;
std::cout << "The area of the circle is " << area << std::endl;
std::cout << "The circumference of the circle is " << circumference << std::endl;
return 0;
}
- Write a program that generates and prints 10 random numbers between 1 and 100.
cCopy code#include <iostream>
#include <cstdlib>
#include <ctime>
int main() {
int i;
srand(time(0));
for (i = 0; i < 10; i++) {
std::cout << 1 + rand() % 100 << std::endl;
}
return 0;
}
These exercises should give you a good starting point for learning C++. Practice writing more programs and experimenting with the language to improve your skills. Good luck!