Lab 3
Lab 3
LAB # 3
Operators
There are various types of operators that may be placed in these categories:
Basic: +, - , *, /, %
Power: ^
Logical: && , || , !
If the value of an item can be changed in the program then it is a variable. If it will not
Change then that item is a constant. The various variable types (also called data type) in
C+ are: int, float, char etc.
Variable declaration:
Type var-name;
For example:-
int a;
int a,b; int a,b,c;
‘int’ is the data type and ‘a’ is a variable name , you can declare more than one variable
at a time by using one same data type.
Sir Syed University of Engineering and Technology
(Department of Computer Science and Information Technology) Page 1
Programming Fundamentals (CS-116) LAB # 3
C++ offers the programmer a rich assortment of built-in as well as user defined data
types. Following table lists down seven basic C++ data types –
Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wchar_t
Several of the basic types can be modified using one or more of these type modifiers –
signed
unsigned
short
long
The following table shows the variable type, how much memory it takes to store the
value in memory, and what is maximum and minimum value which can be stored in such
type of variables.
You have already learned that cout is used to output (print) values. Now we will use cin
to get user input. cin is a predefined variable that reads data from the keyboard with the
extraction operator (>>).
In the following example, the user can input a number, which is stored in the variable x.
Then we print the value of x:
Example
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value
In this example, the user needs to input two numbers, and then we print the sum:
Example
int x, y;
int sum;
cout << "Type a number: ";
cin >> x;
cout << "Type another number: ";
cin >> y;
sum = x + y;
cout << "Sum is: " << sum;
Lab Tasks
1- Write a program that take four floating numbers from keyboard and prints their
sum, product and average.
2- Write a program to calculate the area of the circle, taking the value of the radius
from the user.
3- Write a program that prints to calculate the age in days by using the formula:
days = years * 365.
6- State the order of evaluation the operation in each of the following C++ statements,
and show the value of x after each statement is performed.
a) x = 7 + 3 * 6 / 2 - 1;
b) x = 2 % 2 + 2 * 2 - 2 / 2;
a) root =
b) z =