Search here

Wednesday, July 6, 2011

C++ tutorial: Arithmetic Operators (Part 3)


 :  Hi this is the 3rd part of C+++ beginner tutorial. If you did not check Part 1 and part click the following link for reading them .

For Part 1 - Just Click hereFor part 2 - Just Click here


Arithmetic operator :

There are five arithmetic operators which are used in C++ programming. They are:


For doing basic arithmetic through C++, you need to know about the functions of these arithmetic operators.
The function of all operators does same as those are used in mathematics, except modulus (%) operator. It is used for finding the reminder.
Reminder example:
#include <iostream>
using namespace std;
int main()
{
     int x;
     int z;
     int reminder;
     int y;
     cout <<"enter a number = ";
     cin >> x;
     cout <<" enter another number by which you are going to divide = ";
     cin >>z;
     y = x / z;
     reminder = x % z;
     cout << " after division = " << y <<endl;
     cout << " reminder = "<<reminder <<endl;
     return 0;
}

and the output will be…



·       Compound assignment:
Compound assignments are simply the short form of some arithmetic operators. Some of the compound assignments are given below:

Expression
Equivalent to
A += 5
A = A + 5
A - = 5
A = A – 5
A *= 5
A = A * 5
A /= 5
A = A / 5
A %= 5
A = A % 5
A >>= 5
A = A >> 5
A <<= 5
A = A << 5

Example program:

#include <iostream>
using namespace std;
int main ()
{
       int a;
       int b;
       cout << "enter a number = ";
       cin >>a;
       a +=2;
       b =a;
       cout << b;
       return 0 ;
}

And the output will be…



·       Increase and decrease:
Shortening even more some expressions, the increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively.

Expression
Equivalent to
A++ or ++A
A = A + 1
A - - or - - A
A = A - 1

Example program:
#include <iostream>
using namespace std;
int main ()
{
       int a;
       cout << "enter a number = ";
       cin >>a;
       a++;
       cout << a << endl;
       return 0 ;
}

And the output will be…


·       Rational and equality operators:
To evaluate a comparison between two expressions we can use the rational and equality operators.
Expressions
Meaning
==
Equal to
!=
Not equal to
> 
Greater than
<
Less than
>=
Greater than or equal to
<=
Less than or equal to

If you have Microsoft Visual C++ or any other compiler installed in your computer, you can just check the above programs.




Thank you for visiting my blog

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.