Lab Session 3
Lab Session 3
LAB #3
Operators
There are various types of operators that may be placed in these categories:
Basic: +,-,*,/,%
Power: ^
Relational: <,>,<=,>=,==,!=
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.
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.
Operator Precedence:
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;
In-Lab Tasks
Task#1: Write a program that take four floating numbers from keyboard and prints their
sum, product and average.
Task#2: Write a program to calculate the area of the circle, taking the value of the radius
from the user.
Task#3: Write a program that prints to calculate the age in days by using the formula:
days = years * 365.
Task#4: Write a C++ program to convert temperature from degree Celsius to Fahrenheit.
Post-Lab Tasks
Task 1:
Write a C++ program that takes person’s weight in KG, and convert it into pounds.
Your program should output both weight. (Note that 1 KG 2.2 pounds.)
Expected output:
Example 1:
Enter weight in KG: 69
Value in KG is : 69
Value in pounds is : 151.8
Example 2:
Enter weight in KG: 189
Value in KG is : 189
Value in pounds is : 415.8
Task 2:
A cashier has currency notes of denominations 10, 50 and 100. If the amount to be withdrawn is input through the
keyboard in hundreds, find the total number of currency notes of each denomination the cashier will have to give
to the withdrawer. Write a C++ program to implement the above scenario.
Expected output:
Example 1:
Enter amount in hundreds: 700
Currency note in denominations 10 are: 70
Currency note in denominations 50 are: 14
Currency note in denominations 100 are: 7
Example 2:
Enter amount in hundreds: 1400
Currency note in denominations 10 are: 140
Currency note in denominations 50 are: 28
Task 3:
Write a C++ program that calculates monthly salary of an employee. The salary is calculated after deduction of
following taxes.
Income Tax: 10%
Global Life Insurance: 2.75%
Pension Plan: 2.3%
Health Insurance: 200.0 rupees
Your program should prompt the user to input the gross amount and your program should show each tax and net
salary (after tax deduction). Your output should be in same format (not same values) as shown in example below.
Expected output:
Example 1:
Enter Gross amount of employee(RS. ): 100000
Income Tax (RS.): 10000
Global Life Insurance(RS.): 2750
Pension Plan (RS.):2300
Health Insurance: 200.0 rupees
Net Salary (after tax deduction) (Rs. ): 84750
Task 4:
Write a C++ program that takes a 5-digit number in variable X. Your program should store the reverse of entered
number into variable Y. You program should also print it on screen.
Note: you are not supposed to take 5 separate numbers, but you have to take one number of 5 digits in a single
variable. Also store its reverse in a single variable.
Hint: Use % and / operator
Expected output:
Example 1:
Enter 5 Digit number in Variable X: 57391
5 Digit number in reverse order in Variable Y is:19375