Operators

Shape Image One

OPERATORS

advanced divider

ARITHMETIC OPERATORS:

Arithmetic operators are used to perform mathematical operations. All operators used

integer & floating-point data type except remainder or modulas operator.

Operator

Example

OPERATORS INC++:

Operators are the symbols which tell the computer to execute certain mathematical or

logical operations. A mathematical or logical expression is generally formed with the help

of an operator. C++ programming offers a number of operators which are classified into the

following categories.

Arithmetic Operators

a % b

a/b

a b

a-b

a+b

SIMPLE CALCULATOR PROGRAM IN C++ USING ARITHMETIC OPERATORS

#include<iostream>

#include<conio.h>

#include<stdio.h>

using namespace std;

intmain()

int a, b, add, sub, mul, rem;

float div;

cout<<“\n \t SIMPLE CALCULATOR”;

cout<<“\n \t Enter the value of a…”;

cin >> a;

adamjeecoaching.blogspot.com

cout<<“\n \t Enter the value of a…”;

cin >> b;

add = a+b;

cout<<“\n \t Addition of “<< a << “and” << b

sub-a-b;

cout<<“\n \t Subtraction of “<< a <<

mul = a* b;

<<b<< “is”<< sub;

cout<<“\n \t Multiplication

div = a/b;

cout << “\n \t Division

rem-a % b;

cout<<“\n \t

a << “and” << b << “is”<< mul;

a << “and” << b<< “is” <<div;

ainder of “<< a << “and” << b << “is” << rem;

OUTPUT:

SIMPLE CALCULATOR:

Enter the value of a 30

Enter the value of b 20

Addition of 30 and 20 is 50

Subtraction of 30 and 20 is 10

Multiplication of 30 and 20 600

Division of 30 and 20 is 1

Remainder of 30 and 20 is 10

INCREMENT OPERATORS:

C++ provides the unary increment operator. It is used to be incremented a variable by 1.

Increment operator represented by ++ (double plus sign). The increment operators are

used in two ways (postfix & Prefix) summarized below:

Operators Explanation

++a

(Prefix)

a++

Increment a by 1, then use the new value of a in the expression in

resides.

Use the current value of a in the expression in which a resides, then

(Postfix) increment a by 1.

EXAMPLE (PREFIX) coaching.blogspot.com

#include<iostream>

using namespace std;

int main()

Value of ch is 6

int ch = 5;

cout<<“\n Value of ch is :”<<++cl

return 0;

EXAMPLE (POSTFIX)

cout<<“\n Value of ch is :”<< ch++;

return 0;

OUTPUT

Value of ch is 5

DECREMENT OPERATORS:

C++ also provides the unary decrement operator. It is used to be decremented a variable

by 1. decrement operator represented by — (double minus sign). The decrement operators

are used in two ways (postfix & Prefix) summarized below:

Operators Explanation

-a

(Prefix)

Decrement a by 1, then use the new value of a in the expression which a

resides.

a-

Use the current value of a in the expression in whic

(Postfix) decrement a by 1.

resides, then

RELATIONAL OPERATORS:

Relational operators are used when we have to make comparisons. It is used to test the

relation between two values. The result of comparison is True (1) or False (0). C++

programming offers following relational operators:

Operator

value on left is less than value on right.

the value on left is greater than value on right.

It checks the value on left is less than or equal to value

on right.

It checks the value on left is greater than or equal to

value on right.

Example

a<b

a> b

a <= b

a >= b

It checks the equality of two values.

a == b

It checks the value on left is not equal to value on right.

a != b

PROGRAM USING RELATIONAL OPERATORIN C++:

#include<iostream>

using namespace std;

int main()

int x = 20, y = 10;

if(x > y)

cout << “X is greater than Y”;

else

cout << “Y is greater than X”;

return 0;

OUTPUT

X is greater than Y

adamjeecoaching.blogspot.com

LOGICAL OPERATORS:

Logical operators are used when more than one conditions are to be tested and based on

that result, decisions have to be made. C++ programming offers three logical operators.

They are:

Operator

Operations

Expression

Logical AND. The condition will be true

if both expressions are true.

Logical OR. The condition will be true if

anyone of the expressions are true.

Logical NOT. The condition, will be

inverted, False becomes true & true

becomes false.

1 if a==b && c == d; else 0

1 if a b || c>d; else 0

1 if !(a == 0); else 0

PROGRAM USING LOGICAL OPERATORS INC+

#include<iostream>

#include<conio.h>

using namespace std;

int main()

OUTPUT:

Logical Operators Example

Num1 is less than and Num2 is greater than or

equal to 40

Num 1 or Num 2 is greater than or equal to 40

int num1 = 30, num2 = 40;

cout << “Logical Operators Example \n”;

if(num1<=40 && num2>=40)

cout << “Num1 is less than and Num2 is greater than or equal to 40 \n”;

adamjeecoaching.blogspot.com

if(num1 >=40 | | num2 >= 40)

cout << “Num 1 or Num 2 is greater than or equal to 40 \n”;

getch();

return 0;

DIFFERENTIATE BETWEEN RELATIONAL OPERATOR AND LOGICAL OPERATOR

RELATIONAL OPERATOR

  • Relational operators compare any values in the form of expressions.
  • Relational operators are binary operators because they require two operands to operate.
  • Relational operators return results either 1 (TRUE) or 0 (FALSE).

LOGICAL OPERATOR:

  • Logical operators perform logical operations on boolean values 1 (TRUE) and 0 (FALSE).
  • Logical operator is usually used to compare one or more relational expressions.
  • Logical operator also return output as 1 (TRUE) or 0 (FALSE).

ASSIGNMENT OPERATOR:

Assignment operator (=) are used to assign result of an expression or a value to a variable.

The associativity of assignment operators is right to left means value or expression at the

right is assigned to the left side variable.

ARITHMETIC ASSIGNMENT OPERATOR:

Arithmetic assignment operator is a combination of arithmetic. and assignment operators.

This operator first performs an arithmetic operation on the current value of the variable on

left to the value on the right and then assigns the result to the variable on the left.

OPERATOR

+= (Addition-Assignment)

-=(Subtraction-Assignment)

(Multiplication-Assignment)

/= (Division-Assignment)

DESCRIPTION

Adds the right operand to the left and assigns the

result to the left operand.

Subtracts the right operand to the left and assigns the

result to the left operand.

Multiplies the right operand to the left and assigns the

result to the left operand.

Divides the right operand to the left and assigns the

result to the left operand.

PROGRAM USING ASSIGNMENT & ARITHMETIC ASSIGNMENT OPERATORS IN C+

#include<conio.h>

using namespace std;

int main()

int a = 10;

cout << “Value of a using assignment operator is “<< a << “\n”;

a += 10;

cout << “Value of a using addition assignment operator is “<< a << “\n”;

a -= 10;

cout << “Value of a using subtraction assignment operator is “<< a << “\n”;

a* = 10;

cout << “Value of a using multiplication assignment operator is “<< a << “\n”;

a/= 10;

cout << “Value of a using division assignment operator is “<< a << “\n”;

return 0;

OUTPUT

Value of a using assignment operator is 10

Value of a using addition assignment operator is 20

Value of a using subtraction assignment operator is 10

adamje Value of a using multiplication assignment operator is 100

Value of a using division assignment operator is 10

Quiz

advanced divider