Arithmetic Operators and Operations: Introduction To Computer Programming (EE)
Arithmetic Operators and Operations: Introduction To Computer Programming (EE)
Space Occupied
4 Bytes
4 Bytes
2 Bytes
1 Byte
4 Bytes
8 Bytes
Depends on Compiler
Range
-2147483648 --2147284647
-2147483648 --2147284647
-32768--- 32767
-128 --- 127
3.4x10-38 ----- 3.4x1038
1.7 x10-308 --- 1.7x10308
Const Qualifier:
Const qualifier is used before data type when declaring a constant instead of variable. Variable 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.
Muhammad Hammad
Page 1
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
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
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()
{
Muhammad Hammad
Page 2
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)
b)
c)
d)
e)
f)
6 + 5 * 4 % 3=
12 / 3 * 3 =
10 % 3 6 / 2 =
5.0 * 2.0 / 4.0 * 2.0 =
5.0 * 2.0 / (4.0 * 2.0) =
5.0+ 2.0 / (4.0 * 2.0)=
Page 3
12 / 3 * 3 =
10 % 3 6 / 2 =
5.0 * 2.0 / 4.0 * 2.0 =
5.0 * 2.0 / (4.0 * 2.0) =
5.0+ 2.0 / (4.0 * 2.0)=
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
Muhammad Hammad
Page 4