Operators in C++ Part 1
Introduction
Learning about values, and variables and printing them is not enough for real-world programs, we tend to perform several operations on them.
Let's assume you have 13 candies
and you want to distribute them among 5 students
, How could you find the number of candies each student get and the remaining candies with you?
We will use basic mathematics by dividing and finding the quotient and remainder.
13/5 = 2 candies (Quotient part)
3 Candies left (Remainder Part)
So we have to do some operations between 13 and 5 which are called as values/operands and /
is Divisional Operator.
Operators are symbols that perform operations on variables and values. For example,+
is an operator used for addition, while-
is an operator used for subtraction.
Types of Operators
Operators in C++ can be classified into 6 types:
Arithmetic Operators
Arithmetic operators are used for performing arithmetic operations such as addition, subtraction, etc. Here's a list of operators we will learn in this lesson.
Let's understand it with an example :
#include <iostream>
using namespace std;
int main(){
int number1 = 10;
int number2 = 45;
// Addition
cout << "Sum : " << number1 + number2 << endl;
// Subtraction
cout << "Difference : " << number1 - number2 << endl;
// Multiplication
cout << "Multiplication : " << number1*number2 << endl;
// Division
cout << "Division : " << number2 / number1 << endl;
// Remainder when number2 divided by number1
cout << "Remainder : " << number2 % number1 << endl;
return 0;
}
Output:
Sum : 55
Difference : -35
Multiplication : 450
Division : 4
Remainder : 5
Important Points:
-
45/10 prints 4 not 4.5 just because 45 and 10 both are integer value therefore answer is also only integer part of the actual answer.
-
45%10 leaves a remainder as 5.
-
10%45 would leave a remainder as 10 as no division is done and complete 10 is a remainder.
Division using floating point numbers
#include <iostream>
using namespace std;
int main(){
double number1 = 45.00;
double number2 = 10.00
cout << "Division : " << number1/number2 << endl;
return 0;
}
Output:
Division : 4.50
Now let's see increment and decrement arithmetic operators.
#include <iostream>
using namespace std;
int main(){
int num = 10;
cout << "Original Value : " << num << endl;
num++; // Increment by 1
cout << "Value after increment : " << num << endl;
num--; // decrement by 1
cout << "Value after decrement : " << num << endl;
return 0;
}
Output :
Original Value : 10
Value after increment : 11
Value after decrement : 10
Num value 10 is being increment by 1 makes it 11 and then decrement by 1 leading it to 10 again.
num++ is equivalent to num = num + 1 .
num - - is equivalent to num = num-1 .
Increment and Decrement operator is further classified as :
1 . Post Increment
In post increment value is being initialised or printed firstly and then the value is being incremented by 1.
#include <iostream>
using namespace std;
int main(){
int num = 90;
// Post incrementing
cout << num++ << endl;
cout << num << endl;
return 0;
}
Output:
90
91
Due to post - increment firstly original value of num 90 is being printed and then it is being increment to 91.
2. Pre Increment
In pre increment value is being incremented by 1 firstly then it is being printed or initialised to other variable.
#include <iostream>
using namespace std;
int main(){
int num = 90;
// Pre incrementing
cout << ++num << endl;
cout << num << endl;
return 0;
}
Output :
91
91
Due to pre - increment firstly value is being incremented by 1 to 91 and then printed and original value is now 91.
Quiz
What is the output of the following code?
#include <iostream>
using namespace std;
int main(){
int x = -10;
int y = 8;
cout << x + y ;
return 0;
}
What is the output of the following code?
#include <iostream>
using namespace std;
int main(){
int x = 67;
int a = ++x;
int b = x++;
cout << x << " " << a << " " << b;
return 0;
}
Practice
1 .Create a program to find the area of square having side of 8.7 units.
Solution Code :
#include <iostream>
using namespace std;
int main(){
double sideLength = 8.7;
double area = sideLength * sideLength;
cout << "Area of Square is : " << area;
return 0;
}
2 .Create a program to divide N candies to M number of students. Suppose you have to divide 18 candies among 4 students equally.How many candies will each student get if candies must be divided equally?
And how many candies will be left that cannot be divided?
Solution Code :
#include <iostream>
using namespace std;
int main() {
// your code goes here
int candies = 18;
int studentCnt = 4;
int candiesCntPerStudent = candies/studentCnt;
int remainingCandies = candies % studentCnt;
cout << "Each student will get : " << candiesCntPerStudent << " candies." << endl;
cout << "Candies Left : " << remainingCandies << endl;
return 0;
}
In this lesson we have discussed Arithmetic operators and all its types , in next lesson we will discuss about Relational , Assignment , Logical , Bitwise and some special operators.
Coding is the art of making numbers dance, and in C++, arithmetic operators are the choreographers, orchestrating the graceful steps of calculation