Sie sind auf Seite 1von 4

Introduction to Computer Programming (EE)

Arithmetic Operators and Operations


Most used Data Types
Data Type
Int (Integer)

Space Occupied
4 Bytes

Long ( to store integer value)

4 Bytes

Short (to store integer Value )


Char ( Character )
Float ( For fraction or decimal
point numbers)
Double (for floating number)
Long double (for floating
numbers)

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.

The # Define Directive:


This directive sets up equivalence between an identifier and a text phrase. For example, the line
#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 cant 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>

Muhammad Hammad

Page 1

Introduction to Computer Programming (EE)

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.
2.
3.
4.
5.
6.

a+=10 => a=a+10


a-=5 => a=a-5
a*= 2 => a=a*2
a/=2 => a=a/2
a%=2 => a=a%2

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

Introduction to Computer Programming (EE)


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

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

3. What does the following expression evaluate to?


a) 6 + 5 * 4 % 3=
Muhammad Hammad

Page 3

Introduction to Computer Programming (EE)


b)
c)
d)
e)
f)

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

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

Muhammad Hammad

Page 4

Das könnte Ihnen auch gefallen