You are on page 1of 8

Programming Fundamentals

Lab Manual (Lab 3)

Topic: User Input, Data Types, Escape Sequences,


Operators

Course Instructor:
Lab Instructor:

Session: Spring 2021

School of Systems and Technology


UMT Lahore Pakistan
Objectives
The objective of this lab is to learn about different data types in C++, escape sequences and operators.

C++ Data Types:

There are only a few basic data types in C++ Like:

Data Type Size Description


int 4 bytes Stores whole numbers, without decimals
float 4 bytes Stores fractional numbers, containing one or more decimals.
Sufficient for storing 7 decimal digits
double 8 bytes Stores fractional numbers, containing one or more decimals.
Sufficient for storing 15 decimal digits
boolean 1 byte Stores true or false values
char 1 byte Stores a single character/letter/number, or ASCII values
Data type modifiers available in C++ are: 
 
 Signed
 Unsigned
 Short
 Long

Below is the table having size and ranges of datatypes when combined with the type modifiers:
 
Type Size in bytes Typical Range
char 1byte -127 to 127 or 0 to 255
unsigned char 1byte 0 to 255
signed char 1byte -127 to 127
int 4bytes -2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295
signed int 4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767
unsigned short int 2bytes 0 to 65,535
signed short int 2bytes -32768 to 32767
long int 8bytes -2,147,483,648 to 2,147,483,647
signed long int 8bytes same as long int
unsigned long int 8bytes 0 to 4,294,967,295
long long int 8bytes -(2^63) to (2^63)-1
unsigned long long 8bytes 0 to 18,446,744,073,709,551,615
int
float 4bytes
double 8bytes
long double 12bytes
wchar_t 2 or 4 bytes 1 wide character
Escape Sequences:
Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals
and printers. They are also used to provide literal representations of nonprinting characters and characters that
usually have special meanings, such as the double quotation mark (").

Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called
"escape sequences." Escape sequence can be used to represent a newline character, a horizontal tab or any other
special character. The following table lists some of the ANSI escape sequences and what they represent.

Escape Sequence Represents

\b Backspace

\n New line

\r Carriage return

\t Horizontal tab

\v Vertical tab

\' Single quotation mark


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

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
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:
Lab Tasks
Task 1:

Write a program which takes 2 integers as input from user and prints their sum, product, difference,
product, division and remainder.

Sample Output

Enter first integer100


Enter second integer72
Sum is 172
Difference is 28
Product is 7200
Quotient is 1
Remainder is 28
.

Task 2:

1. Write appropriate C++ statements to match the description in each of the following comments.

1. declare an integer variable for radius


2. declare an integer variable for diameter
3. declare an integer variable for circumference
4. declare an integer variable for area
5. declare a constant integer variable for value of pie
6. prompt user to enter value of radius
7. store that value in radius
8. calculate diameter of circle and store in appropriate variable
9. calculate circumference of circle and store in appropriate variable
10. calculate area of circle and store in appropriate variable

Sample Output

Enter the radius 25


Diameter = 50
Circumference = 157
Area = 1963

Now write the above program again but use float type for radius, diameter, circumference and area. Use
the value 3.14159 for π. What is the new output?
Task 3:

Follow the instructions given below to write a simple program and compile.

Declare the variable named “Base”, “Height” and “Area” initialized by any value. Your program should calculate
the Area of the Triangle. Hint:

Formula:

Area of Triangle = (base*height)/2.

Sample Output

Base is 2
Height is 4
The area for the triangle with the base of 2 and the height 4 is 4.0.

Task 4:

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

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

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 ‘%’).

Task 7:

Write a C++ program to calculate the following equations.

1. b 2−4 ac
2. y = mx + c
Task 8:

Write a C++ Program for the Exercises 1-20, translate the phrase into a mathematical expression
involving the given variable.

width n, length z, demand b, supply y, speed v, numbers p x q

1. 8 times the width n


2. 2 times the length z
3. 6 times the sum of the number n and 3
4. 10 times the sum of the number n and 8
5. the demand b quadrupled Hint: quadrupled means 4 times
6. the supply y quadrupled
7. the speed v decreased by 33
8. the speed v decreased by 30
9. 10 times the width n
10. 10 times the length z
11. 9 times the sum of the number z and 2
12. 14 times the sum of the number n and 10
13. the supply y doubled
14. the demand b quadrupled
15. 13 times the number x sum of the 15 times the number p
16. 14 increased by 5 times the number q
17. 4 decreased by 11 times the number x
18. 13 decreased by 5 times the number p
19. the speed v decreased by 10
20. the speed v increased by 32

Task 8:

Write a C++ program for Conversion Calculator:

Take required values from user the convert the following:

 seconds in minutes
 minutes in hours
 seconds in hours
 days to years
 months to years
 years to century
 centimeters to inches
 centimeters to feet
 inches to feet
 inches to yards
 centimeters to meters
 feet to yards

You might also like