cp - lab02 - data types & operators
Post on 04-Apr-2018
225 Views
Preview:
TRANSCRIPT
-
7/30/2019 CP - Lab02 - Data Types & Operators
1/17
CS 161 - COMPUTER PROGRAMMING
Lab 02
Data Types & Arithmetic Operators
By Kiran Zahra
CS Department, Air University
-
7/30/2019 CP - Lab02 - Data Types & Operators
2/17
Group name: CProg-Fall2012-A
Group home page:
http://groups.yahoo.com/group/CProg-Fall2012-A
Group email:CProg-Fall2012-A@yahoogroups.com
Group Details
http://groups.yahoo.com/group/CProg-Fall2012-Amailto:CProg-Fall2012-A@yahoogroups.commailto:CProg-Fall2012-A@yahoogroups.commailto:CProg-Fall2012-A@yahoogroups.commailto:CProg-Fall2012-A@yahoogroups.commailto:CProg-Fall2012-A@yahoogroups.commailto:CProg-Fall2012-A@yahoogroups.comhttp://groups.yahoo.com/group/CProg-Fall2012-Ahttp://groups.yahoo.com/group/CProg-Fall2012-Ahttp://groups.yahoo.com/group/CProg-Fall2012-Ahttp://groups.yahoo.com/group/CProg-Fall2012-Ahttp://groups.yahoo.com/group/CProg-Fall2012-A -
7/30/2019 CP - Lab02 - Data Types & Operators
3/17
Lab Manuals
-
7/30/2019 CP - Lab02 - Data Types & Operators
4/17
1) Type Conversions
2) Practice Questions
1) Data Types
2)Arithmetic Operators3) Operator Precedence
4) cin, cout
Lab Brief
-
7/30/2019 CP - Lab02 - Data Types & Operators
5/17
Type Conversions
-
7/30/2019 CP - Lab02 - Data Types & Operators
6/17
Type Conversions : Integer and Float
rules that are used for implicit conversion of floating
point and integer values in C++ are;
An arithmetic operation between an integer and integer
always yields an integer result.
Operation between a real and real always yields a realresult
Operation between an integer and real always yields areal result.
-
7/30/2019 CP - Lab02 - Data Types & Operators
7/17
Type Conversions : Integer and Float
Example
int i;
float y;
i = 35.9;
y = 10;
As 35.9is offloat type it cannot be stored in iofinttype. In this case float is demoted to an int and then
value is stored in i. So the value ofiwill be 35.
Same will happen in yi.e. 10 will be promoted to 10.00
and then will be stored in y.
-
7/30/2019 CP - Lab02 - Data Types & Operators
8/17
Type Conversions
float a=1.0, b = 2.0, c = 10.0;
int s;
s = a * b * c / 100 + 32 / 4 3 * 1.1;
In the above example, some of the operands
are of type int and some of them are of typefloat. As we know during evaluation of the
expression int will be promoted to float andresult would be of type float. But when this floatvalue is assigned to s, it is again demoted to anint and then stored in s.
-
7/30/2019 CP - Lab02 - Data Types & Operators
9/17
Type Conversions
In some Cases, it may happen that the type of the
expression on the right hand side and the type of
the variable on the left hand side of the assignment
operator may not be same.
In that case the value of the expression is
promoted ordemoted depending on the type ofthe variable on left hand side of assignment
operator
-
7/30/2019 CP - Lab02 - Data Types & Operators
10/17
Lab Task 01
Take two integer numbers from user and apply
division on them. Store result in float type variable
and display result.
Take two float type numbers from user and apply
multiplication on them. Store result in integer type
variable and display result.
-
7/30/2019 CP - Lab02 - Data Types & Operators
11/17
Practice Questions
-
7/30/2019 CP - Lab02 - Data Types & Operators
12/17
Lab Task 02
-
7/30/2019 CP - Lab02 - Data Types & Operators
13/17
Lab Task 03
Write a program to calculate the following arithmetic
expressions. Analyze the result
-
7/30/2019 CP - Lab02 - Data Types & Operators
14/17
Lab Task 04
Write a program that stores the values of following
expressions in two different variables and analyze
the result. If both are same display Yes on console
otherwise display No
-
7/30/2019 CP - Lab02 - Data Types & Operators
15/17
Lab Task 05
Write a program to find number of bytes occupied
by different data types stated below:
int float
double
bool
char long int
Use sizeof() operator
-
7/30/2019 CP - Lab02 - Data Types & Operators
16/17
Lab Task 06
-
7/30/2019 CP - Lab02 - Data Types & Operators
17/17
Lab Task 07
Write a program that take a number from user and
calculates its square and cube. Suppose user entered 2,
output should be displayed as follows:
17
Value 2Square 4
Cube 8
top related