0% found this document useful (0 votes)
4 views7 pages

PF Y8 Lab 5

Uploaded by

fatimakaneeze11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

PF Y8 Lab 5

Uploaded by

fatimakaneeze11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Programming Fundamentals

Lab – Y8

Lab 5
Lab 5 Programming Fundamental Lab Y8

Contents
Learning basic Operators in C++ ............................................................................................................. 2
Objective ............................................................................................................................................. 2
Operators ............................................................................................................................................ 2
Assignment Operator: ......................................................................................................................... 2
Mathematical Operator: ..................................................................................................................... 2
Unary Operator: .................................................................................................................................. 3
Example of Postfix mode: ............................................................................................................... 3
Example of Prefix mode: ................................................................................................................. 3
Sample Task ........................................................................................................................................ 5
Lab Tasks ................................................................................................................................................. 6
Task 1: ................................................................................................................................................. 6
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. .................... 6
Task 2: ................................................................................................................................................. 6
Task 3: ................................................................................................................................................. 6
Write a program that get your SGPA of 5 courses and calculate your SGPA using following formula.
............................................................................................................................................................ 6
Task 4: ................................................................................................................................................. 6
Task 5: ................................................................................................................................................. 6
Task 6: ................................................................................................................................................. 6

1
Lab 5 Programming Fundamental Lab Y8

Learning basic Operators in C++


Objective

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:

• The assignment operator (=).


• Mathematical operators (+, -, /, *, %).
• Relational operators (>, <, >=, <=, ==,! =)
• 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.

2
Lab 5 Programming Fundamental Lab Y8

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.
++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 multiplications, 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

3
Lab 5 Programming Fundamental Lab Y8

subtraction also have the same level of precedence, which is lower than the precedence
of the multiplication, division and remainder operations.

Order of Associativity
() 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
evaluated left to right.
% Remainder

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

4
Lab 5 Programming Fundamental Lab Y8

Sample Task

Write a C++ program to display Operators c++ and ++c. The input should be given as; c=2 and
d=2.

#include <iostream.h>
using namespace std;
int main()
{
int c=2,d=2;
cout << c++ << endl; //this statement displays 2 then, only c
incremented by 1 to 3.
cout << ++c; //this statement increments 1 to c then, only c is
displayed.
return 0;
}

5
Lab 5 Programming Fundamental Lab Y8

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) x = 7 + 3 * 6 / 2 - 1;
b) x = 2 % 2 + 2 * 2- 2 / 2;
c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / (3) ) ) );
d) x = 2 * 3 / 4 + 4 / 4 + 8 – 2 + 5 / 8;
e) x = 3 / 2 * 4 + 3 / 8 + 3;

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 SGPA of 5 courses and calculate your SGPA using following formula.
𝑡𝑜𝑡𝑎𝑙_𝑒𝑎𝑟𝑛𝑒𝑑
𝑆𝐺𝑃𝐴 =
𝑡𝑜𝑡𝑎𝑙_𝑐𝑟𝑒𝑑𝑖𝑡_ℎ𝑜𝑢𝑟

𝑡𝑜𝑡𝑎𝑙_𝑒𝑎𝑟𝑛𝑒𝑑 = (𝑔𝑝𝑎1 × credit_hour) + (𝑔𝑝𝑎2 × credit_hour) + (𝑔𝑝𝑎3 × credit_hour)


+ (𝑔𝑝𝑎4 × credit_hour) + (𝑔𝑝𝑎5 × credit_hour)

𝑡𝑜𝑡𝑎𝑙_𝑐𝑟𝑒𝑑𝑖𝑡_ℎ𝑜𝑢𝑟 = 𝑐𝑟𝑒𝑑𝑖𝑡_ℎ𝑜𝑢𝑟 × 𝑡𝑜𝑡𝑎𝑙_𝑐𝑜𝑢𝑟𝑠𝑒

Task 4:
Write a program to find the value of distance D for given values of x, v, t, a
1
Where 𝐷 = 𝑥 + 𝑣𝑡 + 2 𝑎𝑡 2

Task 5:
If a five-digit number is an input through the keyboard, write a program to calculate the sum
of its digits (Hint: Use the modulus operator ‘%’).

Task 6:
If a =10, b = 12, c = 0. Find the values of each of the following expressions. Write a program to
calculate and display the 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