You are on page 1of 3

Lab Session 03

Objective
Mathematical Operators, Mathematical Expressions

Theory

Operators:
Operators form the basic foundation of any programming language. Without operators, we
cannot modify or manipulate the entities of programming languages and thereby cannot
produce the desired results. C++ is very rich in built-in operators.

In C++ most of the operators are binary operators i.e. these operators require two operands to
perform an operation. Few operators like ++ (increment) operator are the unary operator which
means they operate on one operand only.

Arithmetic Operators:
• C++operators
‘+’ addition
‘-‘ Subtraction
‘*’ Multiplication
‘/’ Division
‘%’ Mod Operator (remainder)

• Operator Precedence
Operations inside () are evaluated first
*, /, and % are at the same level of precedence and are evaluated next
+ and - and have the same level of precedence and are evaluated last
When operators are on the same level, performed from left to right

Multiple Operators:
• Unary operators:
are those that require only one operand
For example: ++num;

• Binary operators:
operator that operates on two operands
For example: x * y;

C++ provides the modulus operator, %, that yields the remainder after integer division. The modulus
operator can be used only with integer operands. The expression x % y yields the remainder after x is
divided by y. Thus, 7 % 4 yields 3 and 17 % 5 yields 2.

Mathematical Expression
C++ expression consists of operators, constants, and variables which are arranged according to the rules
of the language. It can also contain function calls which return values. An expression can consist of one
or more operands, zero or more operators to compute a value. Every expression produces some value
which is assigned to the variable with the help of an assignment operator.

A constant expression is an expression that consists of only constant values. It is an expression whose
value is determined at the compile-time but evaluated at the run-time. It can be composed of integer,
character, floating-point, and enumeration constants.
#include <iostream>
using namespace std;
int main()
{
int x; // variable declaration.
int y; // variable declaration
int z; // variable declaration
cout<<"Enter the values of x and y";
cin>>x>>y;
z=x+y;
cout<<"\n"<<"Value of z is :"<<z; // displaying the value of z.
return 0;
}

#include <iostream>
#include <string>
using namespace std;

int main()
{
int op1=3,op2=4;
float op3=10.1,op4=5.4;
cout<<"Operands are op1 = "<<op1<<" op2 = "<<op2;
cout<<" op3 = "<<op3<<" op4 = "<<op4;
cout<<endl;
cout<<"op1 + op2 = "<<op1+op2<<endl;
cout<<"op1 - op2 = "<<op1-op2<<endl;
cout<<"op3 * op4 = "<<op3*op4<<endl;
cout<<"op3 / op4 = "<<op3/op4<<endl;
cout<<"op2 % op1 = "<<op2%op1<<endl;
}

The next arithmetic operators are ++ and –. These are called increment and decrement operators
respectively. The increment operator increases the value of the operand by 1 while the decrement
operator decreases the value of the operand by 1.

The expression x++ is equivalent to

x+=1;

x = x+1;

Similarly, the expression x—is equivalent to

x -=1;

x = x-1;

The increment and decrement operators can be placed as a prefix as well as a suffix to the operand.
Depending on its placement, these operators have a different meaning to the evaluation of an expression.

When placed as a prefix, the increment/decrement operation is known as pre-increment or pre-


decrement respectively. When placed as a suffix, the increment/decrement operation is called as post-
increment or post-decrement operation respectively.

Whenever expressions are involved, in case of pre-increment or pre-decrement, the operation


(increment or decrement) is carried out first and then the assignment is done. In the case of post-
increment or post-decrement, the assignment is done first and the operation is carried out after that.
#include <iostream>
#include <string>
using namespace std;

int main()
{
int x=4,y;
y = ++x;
cout<<"PreIncrement:Value of x = "<<x;
cout<<endl;
cout<<"PreIncrement:Value of y = "<<y;
cout<<endl;

y = x--;
cout<<"PostDecrement:Value of x = "<<x;
cout<<endl;
cout<<"PostDecrement:Value of y = "<<y;
cout<<endl;
}
#include <iostream>
using namespace std;
int main(){
int num1 = 240;
int num2 = 40;
num2 = num1;
cout<<"= Output: "<<num2<<endl;
num2 += num1;
cout<<"+= Output: "<<num2<<endl;
num2 -= num1;
cout<<"-= Output: "<<num2<<endl;
num2 *= num1;
cout<<"*= Output: "<<num2<<endl;
num2 /= num1;
cout<<"/= Output: "<<num2<<endl;
num2 %= num1;
cout<<"%= Output: "<<num2<<endl;
return 0;
}

Programming Tasks
1. Write a program that asks the user to enter three numbers (use three separate input
statements). Create variables called total and average that hold the sum and average of
the three numbers and print out the values of total and average.
2. Write a program to swap value of two variables without using third variable.
3. Write a program which accepts days as integer and display total number of years,
months and days in it.
4. Write a program to calculate reverse of a three digit number (first get digits using
modulus Operator and then calculate reverse number

You might also like