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

Class 03 Guide: Arithmetic Expressions

This class guide covers arithmetic expressions in C programming such as addition, subtraction, multiplication, and division. It explains how to perform different types of divisions and assign the results of arithmetic expressions to variables. Students are taught the order of operations using the BODMAS rule and how to use the modulo operator to find remainders of divisions.

Uploaded by

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

Class 03 Guide: Arithmetic Expressions

This class guide covers arithmetic expressions in C programming such as addition, subtraction, multiplication, and division. It explains how to perform different types of divisions and assign the results of arithmetic expressions to variables. Students are taught the order of operations using the BODMAS rule and how to use the modulo operator to find remainders of divisions.

Uploaded by

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

CLASS 03 GUIDE 

 
In this class, we are going to cover the following topics: 
● Arithmetic Expression. 
● Assigning arithmetic expression and their result to variables. 
 
ARITHMETIC EXPRESSIONS: 
Arithmetic Expressions are simple mathematical expressions. There are 5 arithmetic expressions in 
Programming: 
1. Addition → + 
2. Subtraction → - 
3. Multiplication → * 
4. Division → / 
5. Finding the Remainder (Using Modulus) → % 
 
Note: You need to store the results of the above expressions in a variable. For example: 
int chicken = 6 + 7; (which will hold 13 for you inside the chicken variable) 
double duck = 8.0 + 9.0; (which will hold 17.000000 for you inside the duck variable) 
int x = 8 * 7; (which will hold 56 for you inside the x variable) 
double y = 9.0 * 10.0; (You know the drill......) 
int mouse = 10 - 5;  
 
 
 
Division: 
Division in C programming is a bit different. There are two types of division in C: 
1. Decimal Point Division (The normal Division that we do in our everyday life). 
2. Integer Division. 
 
Decimal Point Division: 
Decimal Point Division is the normal division that you guys do in the math classes. For example: 
The answer of 5 divided by 2 would be 2.500000, 4 divided by 3 would be 1.333333 
 
Note: In programming, the decimal points are printed to 6 decimal places. 
 
How do you tell C that it is going to be a decimal point division? 
There are 2 conditions: 
1. The data type of the variable should be double. 
2. One of the numbers should have a decimal point. 
 
Example 01: 
double netflix = 5.0/2; (which will hold 2.500000 for you inside the netflix variable) 
Example 02: 
double hulu = 8/4.0; (which will hold 2.000000 for you inside the hulu variable) 
 
Example 03: 
double hbo = 8.0/4.0; (which will hold 2.000000 for you inside the hbo variable) 
 
Warning: double x = 8/2; won’t work → the result would be 4 instead of 4.000000. 
 
Integer Division: 
integer Division is the normal division without the decimal points. For example: 
The answer of 5 divided by 2 would be 2 instead of 2.5, 4 divided by 3 would be 1 instead of 
1.33333, 1/2 would be 0 instead of 0.50. You just omit the decimal part 
  
Example 01: 
int netflix = 5/2; (which will hold 2 for you inside the netflix variable) 
 
Assigning arithmetic expression and their result to variables: 
 
Simple arithmetic expressions: 
int x = 10 + 6; 
The variable x will now contain 16 inside it. 
 
Complex arithmetic expressions: 
int x = 10 + 6 - 10 / 4 * (4 * 2); 
The variable x will now contain 0 inside it. Let’s break it down: 
 
C also follows the BODMAS (PEMDAS in America) rule for math.  
The expression inside the brackets is evaluated first → (4 * 2) which becomes 8. Now the expression 
becomes:  
10 + 6 - 10 / 4 * 8; 
Next comes the division. Remember this is the integer division as the data type is int. So the value 
of 10 / 4 would be 2 instead of 2.500000. The expression now becomes: 
10 + 6 - 2 * 8; 
You know the rest: 
10 + 6 - 16; 
16 -16; 

Hence the value of x is 0. 
 
 
 
 
 
You can also store values in variables and do arithmetic expressions with them: 
For example: 
int x = 9; 
int y = 10; 
int z = x + y; 
z will now have the value 19 stored inside it. 
Another example would be: 
int x = 10; 
int z = x + 5; 
The above is also valid. As you have guessed z has the value 15 in it. The breakdown would be: 
Take the value of x and add 5 to it and then assign the added value to z. 
 
Doing arithmetic expressions and assigning it to the same variable: 
 
Suppose you have the following value in a variable: 
int x = 19; 
Now you want to add 10 to x and still store it into x. How would you do that? It’s very simple: 
x = x + 10; 
Let’s break it down: 
So it goes like this. C always evaluates the right-hand side of the expression first that is it evaluates 
the x + 10 first then goes to the left. In short, the order of operation is always from right to left 
(unlesssssssssssssss BODMAS is applied in the rule). So in the above expression, C sees it like 
this: take the current value of x (which is 19) and add 10 to it. After that assign the new value 
(which is 29) to x (the left side of the =). So now, x contains 29 instead of 19. 
For you to try: try printing the value of x before and after adding 10. 
 
The Modulo Operator for finding remainders → % : 
Every programming language has a special arithmetic operator named modulo denote by %. 
The modulo operator is used to find the remainders of a division. For example, if you divide 5 by 2 
the remainder is 1; if you divide the 100 by 34 the remainder is 32. 
 
Note: All remainders are of int datatype. 
 
How to do it in C? 
int x = 5 % 2; 
The value of x would be 1. 
 
Example 02: 
int x = 100 % 34; 
The value of x would be 32. 
 
 
 
Example 03: 
int x = 4; 
int y = x % 2; 
The value of x has not changed so it is still 4 but the value of y is 0 as the remainder of 4 divided by 
2 is 0. 
 
------------------------------------------------------------------------------------------------------------------------------------------------------ 

You might also like