Programming Fundamentals Lab 03 (Arithmetic Operators)
Programming Fundamentals Lab 03 (Arithmetic Operators)
Lab Manual 3
1
Contents
Objectives 3
Operators 3
Assignment Operator:.......................................................................................................................3
Mathematical Operator: 3
Unary Operator:................................................................................................................................3
Operator Precedence:.......................................................................................................................4
Sample Program 1: 6
Lab Tasks 7
Task 1:................................................................................................................................................7
Task 2:................................................................................................................................................7
Task 3:................................................................................................................................................7
Task 4:................................................................................................................................................7
Task 5:................................................................................................................................................7
Task 6:................................................................................................................................................8
2
Objectives
The objective of this lab is to learn the basic arithmetic operators of C language and also to
familiar with operator precedence. At the end of this lab students will be to solve any kind of
expression.
Operators
Operator is a symbol that instructs C/C++ to perform some operation, or action, on
one or more operands.
Operand is something that an operator acts on.
For example:
x = 2 + 3;
+ and = are operators. 2 and 3 are operands and x is variable.
Assignment Operator:
x = y;
Assign the value of y to variable x and this is called assignment statement.
Left side must be a variable name.
Right side can be any expression.
The evaluation from right to left.
Mathematical Operator:
Mathematical operators perform mathematical operation such as +, -, *, % and /. C/C++ also
has unary mathematical operators (++ and --) and binary mathematical operators.
Unary Operator:
Unary operators are named unary because they take a single operand as shown in table
These operators can be used only with variables not with constants. These are used to add 1
or to subtract 1 from the operand.
3
++x same as x = x + 1
--y same as y = y + 1
++x and --y are called prefix mode, that means the increment or decrement operators
modify their operand before it is used.
x++ and y-- are called postfix mode, the increment or decrement operators modify
their operand after it is used.
Operator Precedence:
C applies the operators in arithmetic expressions in a precise sequence determined by the
following rules of operator precedence, which are generally the same as those in algebra:
4
Operator(s) Operation(s) Order of evaluation (precedence)
() Parentheses Evaluated first. If the parentheses are nested, the
expression in the innermost pair is evaluated first.
If there are several pairs of parentheses "on the
same level" (i.e., not nested), they're evaluated
left to right.
++/-- Increment/decrement Evaluated second.
* Multiplication
Evaluated third. If there are several, they're
/ Division
% Remainder evaluated left to right.
+ Addition Evaluated last. If there are several, they're
- Subtraction evaluated left to right.
If the operators are in the same level, then, the operators are performed from left to right
order, referring to table. For example:
5
Sample Program 1:
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
a =10;
b =12;
c = a + b;
cout<<a++<<" : "<<b++<<" : "<<c++<<endl;
cout<<++b<<" : "<<c++<<" : "<< ++a<<endl;
cout<<++c<<" : "<<a++<<" : "<< b++<<endl;
cout<<a--<<" : "<<--b<<" : "<< --c<<endl;
return 0;
}
6
Lab Tasks
Task 1:
State the order of evaluation of the operators in each of the following C statements and show
the value of x after each statement is performed. Write program to check your answers.
a = 7 + 3 * 6 / 2 - 1;
b = 2 % 2 + 2 * 2- 2 / 2;
c = ( 3 * 9 * ( 3 + ( 9 * 3 / (3) ) ) );
d = 2 * 3 / 4 + 4 / 4 + 8 – 2 + 5 / 8;
e = 3 / 2 * 4 + 3 / 8 + 3;
f = ((((4 * 2 + 4 / 3 + 5 * 4 – 3 / 4 + 9) % 10 ) % 5 ) % 2 )
Task 2:
Write a program that reads two values in variable x and y, print values of x and y on the screen.
Then exchange the value of x and y and print the new values after the exchange.
Task 3:
Write a program that get your gpa of 5 courses and calculate your CGPA using following
formula.
CGPA = total_earned / total_credit_hour
Task 4:
Write a program to find the value of distance D for given values of x, v, t, a
Where D= x + vt + 1/2at2
Task 5:
If a five-digit number is input through the keyboard, write a program to calculate the sum of
its digits.(Hint: Use the modulus operator ‘%’).
7
Task 6:
If a =10, b = 12, c = 0. Find the values of each of the following expression. Write a program
to calculate and display value of each expression.
1. a++ + ++a;
2. b = b++ + b-- + a++ + ++a
3. c = (c++ * b-- - ++a) / --c
4. a = (c++ % ++b) + --a + a--