Lab 2
Lab 2
Try This
// Include the string library
#include <string>
Const Qualifier:
Const qualifier is used before data type when declaring a constant instead of variable. Value of
constant remain same and fixed throughout the life of program.
E.g. const float pi = 3.1415; any attempt to alter the variable defined in this way will produce a
compilation error.
#define PI 3.14159
Appearing at the beginning of your program specifies that the identifier PI will be replaced by
the text 3.14159 throughout the program. This construction has long been popular in C.
However, you can’t specify the data type of the constant using #define
Arithmetic Operators
Basic arithmetic operator used are +, - , *, / for addition, subtraction, multiplication and division
respectively. They work with all data types. There is a 5th arithmetic operator called modulus
which is used to find the reminder of two numbers. ‘%’ symbol is used for that ans it works only
with integers.
Urinary Operators
1. a++ => a=a+1
2. a-- => a=a-1
Similarly
3. ++a => a=a+1
4. –a => a=a-1
Example 1
T=a * ++c
Which operation will occur first? C will get incremented first because and then multiplication
will be performed because of prefix notation. On the other hand if postfix notation is used,
multiplication will occur first and then variable will be incremented.
Example 2
int main()
{
int count = 10;
cout << “count=” << count << endl;
cout << “count=” << ++count << endl;
cout << “count=” << count << endl;
cout << “count=” << count++ << endl;
cout << “count=” << count << endl;
return 0;
}
Exercise Questions:
1. Using the example 2, write that program and display the outputs after each
statement with comments.
a) 6 + 5 * 4 % 3=
b) 12 / 3 * 3 =
c) 10 % 3 – 6 / 2 =
d) 5.0 * 2.0 / 4.0 * 2.0 =
e) 5.0 * 2.0 / (4.0 * 2.0) =
f) 5.0+ 2.0 / (4.0 * 2.0)=
a) int n = -100;
b) int 2k;
c) double x = 2 * 10;
d) float y = y * 2;
e) double z = 0.0;
f) double d = 0.67F;
g) float f = 0.52L;
h) char c = '$' + 2;
int n1,n2;
float n3,n4;
1. n1 = ( n3 = (n2 = 5) * 4 / 8.0 ) * 2;
cout << n1 << endl << n2 << endl << n3 << endl;
2. n1 = ( n3 = (n2 = 5) * 4 / 8 ) * 2;
cout << n1 << endl << n2 << endl << n3 << endl;
3. n1 = ( n3 = (n2 = 5) * (4 / 8.0) ) * 2;
cout << n1 << endl << n2 << endl << n3 << endl;
4. n1 = ( n3 = (n2 = 5) * (4 / 8) ) * 2;
cout << n1 << endl << n2 << endl << n3 << endl;
5. Write a program that asks how many miles you have driven and how many
gallons of gasoline you have used and then reports the miles per gallon your
car has gotten. The program can request distance in kilometers and petrol in
liters and then report the result in liters per 100 kilometers
6. Write a program that requests the user to enter the current world population
and the current population of the U.S. (or of some other nation of your
choice). Store the information in variables of type int. Have the program
display the percent that the U.S. (or other nation’s) population is of the
world’s population. The output should look something like this:
Enter the world's population: 6888
Enter the population of the US: 310
The population of the US is 4.5% of the world population