0% found this document useful (0 votes)
15 views

Lab 2

Uploaded by

degag64086
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lab 2

Uploaded by

degag64086
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction to Computer Programming (EE)

Lab 02: Arithmetic Operators and Operations

Most used Data Types


Data Type Space Occupied Range
Int (Integer) 4 Bytes -2147483648 ---
2147284647
Long ( to store integer value) 4 Bytes -2147483648 ---
2147284647
Short (to store integer Value ) 2 Bytes -32768--- 32767
Char ( Character ) 1 Byte -128 --- 127
Float ( For fraction or decimal 4 Bytes 3.4x10-38 ----- 3.4x1038
point numbers)
Double (for floating number) 8 Bytes 1.7 x10-308 --- 1.7x10308
Long double (for floating Depends on Compiler
numbers)

Try This
// Include the string library
#include <string>

// Create a string variable


string greeting = "Hello";

// Output string value


cout << greeting;

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.

The # Define Directive:


This directive sets up equivalence between an identifier and a text phrase. For example, the line

Muhammad Hammad Page 1


Introduction to Computer Programming (EE)

#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

Header files required:


# include<iostream.h>
#include<conio.h>
#include<math.h> or #include<cmath.h>

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.

E.g. Remainder of 5 and 2 is, 5%2 = 1 , 10 %8 = 2, 9 %8 = 1, 8%8=0

Arithmetic assignment operators:


1. a+=10 => a=a+10
2. a-=5 => a=a-5
3. a*= 2 => a=a*2
4. a/=2 => a=a/2
5. a%=2 => a=a%2
6.

Urinary Operators
1. a++ => a=a+1
2. a-- => a=a-1
Similarly
3. ++a => a=a+1
4. –a => a=a-1

Both decelerations increment and decrement variable.

Example 1

Muhammad Hammad Page 2


Introduction to Computer Programming (EE)

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;
}

Same applies for decrementing operator i.e. a- - and - -a

Operator precedence from Left to right:

1. Parenthesis () , ++ postfix version of increment /Decrement


2. Prefix version of increment/Decrement, ^ for power, bitwise complement ~,
logical not!
3. *, / , %
4. + , -
5. Equality ==, not equal! =
Etc

Muhammad Hammad Page 3


Introduction to Computer Programming (EE)

Exercise Questions:

1. Using the example 2, write that program and display the outputs after each
statement with comments.

2. What does the following expression evaluate to?

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)=

3. Which of the following represent valid variable definitions?

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;

4. Evaluate the following assignment statements

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;

Muhammad Hammad Page 4


Introduction to Computer Programming (EE)

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

Muhammad Hammad Page 5

You might also like