Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CS111 Programming Fundamentals

Lab 02: Datatypes and Operators in C++

Lab Session
Objectives:
1. Learning data types, variables, constants, arithmetic operators in C++

Lab Description:

1. Data Types:

A variable provides us with named storage that our programs can manipulate. Each variable in

C++ has a specific type, which determines the size and layout of the variable's memory, this type

is known as data type of that variable.

The name of a variable can be composed of letters, digits, and the underscore character. It must

begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++

is case-sensitive

2. Fundamental Arithmetic Operators:

Expressions:
Multiplication, mode and division have higher precedence than addition and subtraction.
Associativity: left to right.
Implicit conversion (coercion)
CS111 Programming Fundamentals

Lab 02: Datatypes and Operators in C++

Implicit conversions are automatically performed when a value is copied to a compatible type. For
example
3. Type Casting
C++ is a strong-typed language. Many conversions, especially those that imply a different
interpretation of the value, require an explicit conversion, known in C++ as typecasting. There
exist two main syntaxes for generic typecasting: functional and c-like:
double x = 10.3;
int y;
y = int (x); // functional notation
y = (int) x; // c-like cast notation

LAB EVALUATIONS
Task # 1:
Write the output of the following code

Write a program to declare two integers, assign them random values, perform the arithmetic
operations on them (+,-,*,/) and display the result on the screen as shown in sample output.
Sample Output:
Suppose A=10 and B=5
Output
Addition of 10 and 5 = 15
Subtraction of 10 and 5 = 5
10*5=50
10/5=2
Task # 3:
Assume a program has the following variable definitions:
int a, b = 2; float c = 4.2;
and the following statement: a = b * c;
CS111 Programming Fundamentals

Lab 02: Datatypes and Operators in C++

What value will be stored in a?


A. 8.4
B. 8
C. 0
D. None of the above
Task # 1:

Write a program that takes two integer values from user and store them in variables. Swap the
values of both the variables.
Sample Output:
Enter First Number? 10
Enter Second Number? 20
After Swapping
First Number=20
Second Number=10

You might also like