0% found this document useful (0 votes)
16 views4 pages

Arithmetic Operators in Programming

This document discusses data types, arithmetic operators, and arithmetic operations in C++ programming. It includes: 1) An overview of common data types (int, long, short, char, float, double) and the typical range and space occupied for each. 2) Explanations of const qualifier and #define directive for defining constants. 3) Descriptions of basic arithmetic (+, -, *, /, %) and assignment (=, +=, -=, *=, /=, %=) operators. 4) Details on unary increment/decrement (++, --) operators and operator precedence. 5) Examples and exercise questions involving arithmetic expressions and evaluating variables.

Uploaded by

ArslanZafar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Arithmetic Operators in Programming

This document discusses data types, arithmetic operators, and arithmetic operations in C++ programming. It includes: 1) An overview of common data types (int, long, short, char, float, double) and the typical range and space occupied for each. 2) Explanations of const qualifier and #define directive for defining constants. 3) Descriptions of basic arithmetic (+, -, *, /, %) and assignment (=, +=, -=, *=, /=, %=) operators. 4) Details on unary increment/decrement (++, --) operators and operator precedence. 5) Examples and exercise questions involving arithmetic expressions and evaluating variables.

Uploaded by

ArslanZafar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

Common questions

Powered by AI

The program segment begins with count initialized to 10. The first line outputs 'count=10'. The second line uses a prefix increment (++count), which increments count before printing, resulting in 'count=11'. The third line outputs the current count value 'count=11'. The fourth line uses a postfix increment (count++); it first prints the current count 'count=11', then increments it. The final line prints the updated count 'count=12' .

Using 'float', which occupies 4 bytes and offers 7 decimal digits of precision, in high-precision applications can lead to significant accuracy loss and rounding errors due to limited precision . 'double', occupying 8 bytes with 15 decimal digits of precision, provides more accurate results and is preferred in cases requiring high precision such as scientific computations . Precision issues occur because storing larger or more precise floating-point numbers in a 'float' may result in truncated or rounded values, exacerbating errors in complex calculations.

In the expression 'n1 = (n3 = (n2 = 5) * 4 / 8.0) * 2;', operator precedence dictates the order of evaluation. The assignment operator '=' has the lowest precedence and is evaluated last. The multiplication '*' and division '/' operators have higher precedence and are evaluated from left to right. Therefore, 'n2 = 5' is evaluated first, setting n2 to 5. Then 'n2 * 4' or '5 * 4' = 20 is calculated. This result is divided by 8.0, resulting in 2.5, which is assigned to n3. Finally, n3 is multiplied by 2, yielding n1 = 5.0 . Hence, n1 = 5.0, n2 = 5, and n3 = 2.5.

The modulus operator '%' is used to compute the remainder of the division of two integers, unlike addition, subtraction, multiplication, and division that perform their respective arithmetic computations. The modulus operator only works with integer data types. For example, 5 % 2 results in 1, because 5 divided by 2 leaves a remainder of 1; while 10 % 8 results in 2, since 10 divided by 8 leaves a remainder of 2 . It cannot be used with floating-point numbers, differentiating its application from the other arithmetic operators.

The 'const' qualifier in C programming is used to declare a constant variable whose value cannot be altered during the execution of the program. It ensures that the compiler enforces immutability for the variable . In contrast, the '#define' directive creates a macro that replaces the identifier with a specified text value throughout the code. However, '#define' does not provide type safety or allow for memory allocation and cannot be used to declare typed constants .

Type casting in C affects how expressions with mixed data types are evaluated by converting one data type to another, potentially altering the expression's result. In expressions involving different types, implicit type conversion to a common type may occur, impacting precision. For example, in 'n1 = (n3 = (n2 = 5) * 4 / 8.0) * 2;', '(5 * 4)' yields an integer 20, and the division by 8.0 results in a floating-point arithmetic, converting 20 to 20.0. This conversion ensures that the division's result is precise as a float (2.5) rather than integer division which would truncate to 2 .

Selecting inappropriate data types for storing values can lead to overflows or unexpected behavior due to range limitations. For instance, storing a value outside the range of 'short' (-32768 to 32767) in a 'short' variable can result in overflow, wrapping around to a negative start from the positive endpoint or vice versa, leading to incorrect program logic . Similarly, using an 'int' or 'long' (sharing the same range -2147483648 to 2147483647) for values beyond their range causes similar issues. Therefore, understanding data type ranges ensures programmers allocate adequate memory and prevent logical errors.

Using '#define' for constants is advantageous due to its simplicity in substituting text throughout the program, potentially enhancing readability for straightforward replacement . However, '#define' lacks type safety and does not provide variable-like properties, making debugging and readability harder in complex scenarios. In contrast, 'const' variables maintain type information and restrict modifications, which aids debugging and understanding but may reduce readability where simple textual substitutions suffice. 'const' ensures compiler enforcement of constant values, reducing runtime errors linked to unintended changes .

In 'T=a * ++c', the prefix increment operator '++c' increments the value of c before the multiplication operation occurs. Thus, c's incremented value is used in the multiplication with a . Conversely, if the postfix increment 'c++' were used, the multiplication would occur using the current value of c, and only after the multiplication would c be incremented. This difference illustrates how prefix increments affect the current operation, while postfix increments defer the increment until after the current operation's execution.

For '6 + 5 * 4 % 3', multiplication and modulus operators have higher precedence than addition, thus '5 * 4' is evaluated first, giving 20; then '20 % 3' is calculated, resulting in 2; finally '6 + 2' equals 8 . For '12 / 3 * 3', division and multiplication have the same precedence and are evaluated from left to right: '12 / 3' equals 4, then '4 * 3' equals 12. For '10 % 3 - 6 / 2', modulus and division have higher precedence and are evaluated first from left to right: '10 % 3' results in 1, '6 / 2' results in 3, and finally '1 - 3' equals -2 .

You might also like