In this lesson, we'll delve into the world of C++ arithmetic operations: +, -, *, and more! Whether you're a complete beginner or looking to brush up on your skills, this tutorial is designed to make learning enjoyable and practical. Let's get started!
Before we dive into arithmetic operations, let's discuss variables. Variables are used to store data in C++.
int a = 10; // Integer variable
float b = 5.5; // Floating-point variable
char c = 'A'; // Character variableš Note: Always declare the type of variable you're using (int, float, char, etc.) to avoid confusion and potential errors.
+ š”The + operator is used to add numbers or concatenate strings.
int a = 5;
int b = 3;
int sum = a + b; // sum = 8
char str1 = 'H';
char str2 = 'e';
char str3 = 'l';
char greeting[5] = {str1, str2, str3, '\0'}; // Initializing an array with a null character to mark the end of the string- š”The - operator is used to subtract numbers or invert the sign of a number.
int a = 10;
int b = 5;
int diff = a - b; // diff = 5
int negative = -5; // A negative integer* š”The * operator is used to multiply numbers.
int a = 5;
int b = 3;
int product = a * b; // product = 15Which of the following is a valid C++ variable declaration?
Stay tuned for our next lesson, where we'll explore more advanced C++ arithmetic operations and other essential concepts! š