You are on page 1of 8

Programming Fundamentals

Lab Manual 3

Topic: Learning basic arithmetic operators in C++

Lab Instructor: Ahmad Abduhu

Session: Fall 2018

School of Systems and Technology


UMT Lahore Pakistan

1
Contents
Objectives 3

Operators 3

Assignment Operator:.......................................................................................................................3

Mathematical Operator: 3

Unary Operator:................................................................................................................................3

Example of Postfix mode:..................................................................................................................4

Example of Prefix mode:....................................................................................................................4

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.

 Operators fall into several categories as follows:


1. The assignment operator (= ).
2. Mathematical operators (+, -, /, *, %).
3. Relational operators (>, <, >=, <=, ==, !=).
4. Logical operators (AND, OR, NOT, &&, ||).

In this lab we will discuss only Mathematical and Assignment operators.

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.

Example of Postfix mode:


x = 10;
y = x++;
After these statements are executed, x = 11, y has the value of 10, the value of x was assigned
to y, and then x was incremented.

Example of Prefix mode:


y = 10;
y = ++x;
Both y and x having the value of 11, x is incremented, and then its value is assigned to y.

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:

1. Operators in expressions contained within pairs of parentheses are evaluated first.


Thus, parentheses may be used to force the order of evaluation to occur in any
sequence you desire. Parentheses are said to be at the "highest level of precedence." In
cases of nested, or embedded, parentheses, such as ( ( a + b ) + c ) the operators in
the innermost pair of parentheses are applied first.

2. Multiplication, division and remainder operations are applied first. If an expression


contains several multiplication, division and remainder operations, evaluation
proceeds from left to right. Multiplication, division and remainder are said to be on the
same level of precedence.

3. Addition and subtraction operations are evaluated next. If an expression contains


several addition and subtraction operations, evaluation proceeds from left to right.
Addition and subtraction also have the same level of precedence, which is lower than
the precedence of the multiplication, division and remainder operations.

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

 Total_earned = (gpa1*credit_hour ) + (gpa2*credit_hour ) + (gpa3*credit_hour ) +


(gpa4*credit_hour ) + (gpa5*credit_hour );

 Total_credit_hour = credit_hour * total_course;

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--

You might also like