Anda di halaman 1dari 17

CS 161 - COMPUTER PROGRAMMING

Lab 02 Data Types & Arithmetic Operators

By Kiran Zahra CS Department, Air University

Group Details

Group name: CProg-Fall2012-A Group home page: http://groups.yahoo.com/group/CProg-Fall2012-A Group email: CProg-Fall2012-A@yahoogroups.com

Lab Manuals

Lab Brief
1)

2)

Type Conversions Practice Questions 1) Data Types 2) Arithmetic Operators 3) Operator Precedence 4) cin, cout

Type Conversions

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

Operation between an integer and real always yields a real result.

Type Conversions : Integer and Float

Example

int i; float y; i = 35.9; y = 10;

As 35.9 is of float type it cannot be stored in i of int type. In this case float is demoted to an int and then value is stored in i. So the value of i will be 35. Same will happen in y i.e. 10 will be promoted to 10.00 and then will be stored in y.

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 type float. As we know during evaluation of the expression int will be promoted to float and result would be of type float. But when this float value is assigned to s, it is again demoted to an int and then stored in s.

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 or demoted depending on the type of the variable on left hand side of assignment operator

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.

Practice Questions

Lab Task 02

Lab Task 03
Write a program to calculate the following arithmetic expressions. Analyze the result

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

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

Lab Task 06

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:
Value 2

Square Cube

4 8

17

Anda mungkin juga menyukai