{ads}

6/recent/ticker-posts

Arithmetic Operators and Expressions in CPP


 

Operator:  

 An operator is a symbol or letter used to indicate specific operations to be carried out on a variable in a program. For example, the symbol ' + ' is an add operator that adds two items called an operand.

 Expressions:   

An expression is a combination of operands (i.e constants, variables, numbers, etc) connected by operators and parenthesis. For example in the expression given below 'A' and 'B' are operands and '+' is operands.

C++ supports three basic types of operators:
  • Arithmetic operator
  • Relational operator
  • Logical operator

Arithmetic operators:

An expression that performs basic (Addition, Subtraction, Multiplication, Division ) operations on operands are called arithmetic operators.
The valid arithmetic  operators are:



Unary Arithmetic operators
When any operator performs an operation on one operand is called a unary operator. The unary arithmetic operators only supported by c++   are increment and decrement. As compared to binary operators the unary operators are right-associative in the sense that they evaluate from left to right
The unary minus operator is written before a numerical value, variable, or expression.
 An example of usage of the unary minus operator is given below:

  • -57
  • -2.923
  • -x
  • -(a*b)
  • *(-(a+b))
It might be noted here that the consequence of the use of unary less on an operand is the refutation of its operand.
The ' ++ ' and  ' -- ' are unique to C and C++ .These are called increment and decrement operators, respectively. The increment operator ++ adds to its one operand Therefore we can say that the following expressions are equal
i = i + 1;
++i;
For example, If the initial value of i is 10 then the expression ++i will increment the contents of i. Similarly the decrement operator -- subtracts 1 from its operand .we can say that the following expression is equal.
 j = j - 1;
--j;
As long as the increment or decrement operator is not used as a part of expression the prefix and postfix forms of these operators do not make any difference. For example, ++x and x++ produced the same result. However, on the off chance that such an Operator is an aspect of the articulation, at that point prefix and postfix structures would be created completely various outcomes

Example program:

#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter number 1:";
cin>>a;
cout<<"Enter number 2:";
cin>>b;
cout<<"Press"<<endl;
cout<<"1-Addition"<<endl;
cout<<"2-Subtraction"<<endl;
cout<<"3-Multiplication"<<endl;
cout<<"4-Division"<<endl;
cout<<"5-Remindar"<<endl;
cout<<"Enter your choice:";
cin>>c;
switch(c)
{
case 1:
cout<<"Addition is"<<a+b;
break;
case 2:
cout<<"Subtraction is"<<a-b;
break;
case 3:
cout<<"Multiplication is"<<a*b;
break;
case 4:
cout<<"Division is"<<a/b;
break;
case 5:
cout<<"Remindar is"<<a%b;   
}
}

Post a Comment

0 Comments