![]() |
| There are following arithmetic operators supported by C++ language |
Arithmetic Operators:
The use of arithmetic operators in c++ programming language must include care to avoid unclear statements.
The % modulus operator will divide the first given number by the second given number and return the remainder of the operators.
Let's take a look at the program below and execute it to see the out for ourselves.
#include<iostream>
When the above code is compiled and executed, it produces the following result:
a = b * c - d % e / f ; // this is unclear
a = (b*c) - ((d % e) / f); // this is clear
This is useful to determine if a number has an odd or even value
The ++ increment operator and -- operator alter the given value by and returns the resulting new value. These are most commonly used to count iterations in a loop. The ++ mainly increases a value by one either before it is used in the program line or after.
They can both be placed before(prefix) or after(postfix) the operand. if ++ or -- is placed before the operand, it immediately changes the value before it is used in the program.
On the other hand, placing it after the operand, the value is used first and increment or decrement by is added to the result.
usingnamespace std;
main()
{
int a =21;
int b =10;
int c ;
c = a + b;
cout <<"Line 1 - Value of c is :"<< c << endl ;
c = a - b;
cout <<"Line 2 - Value of c is :"<< c << endl ;
c = a * b;
cout <<"Line 3 - Value of c is :"<< c << endl ;
c = a / b;
cout <<"Line 4 - Value of c is :"<< c << endl ;
c = a % b;
cout <<"Line 5 - Value of c is :"<< c << endl ;
c = a++;
cout <<"Line 6 - Value of c is :"<< c << endl ;
c = a--;
cout <<"Line 7 - Value of c is :"<< c << endl ;
return0;
}
Line1-Value of c is:31
Line2-Value of c is:11
Line3-Value of c is:210
Line4-Value of c is:2
Line5-Value of c is:1
Line6-Value of c is:21
Line7-Value of c is:22

1 Comments
Great work comes from within ......
ReplyDeleteDear reader, if you think of a different or an alternative way to achieve this solution, kindly share you idea in the comment section. Thank you.