2.Assignment Operators
Assignment Operators are mainly used to assign the value. It uses the right to left assignment.
The operators
are listed below.
‘=’ – Assigns
a value.
‘+=’ -Add
the value and assign it.
‘-=’ –
subtract the value and assign the value.
‘*=’ –
multiply it and assign the value.
‘/=’ –
divide and assign.
Eg:
x+=y means x=x+y;
x-=y means x=x-y;
x*=y means x =x*y;
x/=y means x=x/y;
A program displays the functionality of assignment operators
#include
<iostream>
using
namespace std;
int x = 18, y = 3;
cout << "x = " << x
<< endl;
cout << "y = " << y
<< endl;
// Addition and Assignment Operator.
cout << "x += y is "
<< (x += y) << endl;
// Subtraction and Assignment Operator.
cout << "x -= y is "
<< (x -= y) << endl;
// Multiplication and Assignment Operator.
cout << "x *= y is "
<< (x *= y) << endl;
// Division and Assignment Operator.
cout << "x /= y is "
<< (x /= y);
return 0;
}
Output:
x = 18
y = 3
x += y is
21
x -= y is
18
x *= y is
54
x /= y is
18
This is the
way of using assignment operators. Hope, this code will help you to know about assignment operators. Keep coding!!!!
Comments
Post a Comment