You are on page 1of 16

Unit 2

Aritmetic Operations

"Good things come to people who wait, but better things come
to those who go out and get them."
--Anonymous
Internal
Arithmetic Operations
• Assignment operator
• Arithmetic operators
• Increment/decrement operators
• scanf() input function
• Order of arithmetic operations

Internal
Assignment Operator
• Basic format:
variable = expression;
“assignment statement”
• Examples:
x = 1;
abc = 123.45;
half = number/2;

Internal
Assignment Operator (2)

expression

variable

Internal
Arithmetic Operators
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder of division

Internal
Arithmetic Operators (2)

Internal
Remainder Operator (%)

• Divide 5 by 2 and the remainder is 1


• result = 1
Internal
Increment and Decrement Operators

• Increment (++)
– Post-increment: x++ ….. x = x + 1
– Pre-increment: ++x ..... x = x + 1

• Decrement (--)
– Post-decrement: x-- ….. x = x + 1
– Pre-increment: --x ..... x = x - 1

Internal
Increment and Decrement Operators (2)

Internal
Increment and Decrement Operators (3)

 Post-increment:
• x++; ..... x = x + 1;
• y = x++; ..... y = x; x = x + 1;

 Pre-increment:
• ++x; ..... x = x + 1;
• y = ++x; ..... x = x + 1; y = x;

Similar for post/pre-decrement


Internal
Input Function scanf()

Internal
Input Function scanf() (2)
Basic format:
scanf("format specifiers", variables);

Examples:
• scanf("%d", &x); // Read a decimal integer into variable x
• scanf("%i", &x); // Read a decimal integer into variable x

• scanf("%f", &val); // Read a floating point number into


// variable val

• scanf("%c", &ch); // Read a character into variable ch


• scanf("%s", name); // Read a string of characters into
// character array variable name

Internal
Input Function scanf() (3)
/* Program 2.6 - This program reads two numbers from the keyboard
and displays the sum on the monitor screen
*/
#include <stdio.h>

int main(void)
{
int first, second, sum;

printf("Enter a number: ");


scanf("%d", &first);
printf("Enter a number: ");
scanf("%d", &second);

sum = first + second;

printf("\n%d + %d = %d", first, second, sum);

return 0;
Internal }
Order of Operations - BODMAS
"Operations" mean things like add, subtract, multiply, divide, squaring, etc.
If it isn't a number it is probably an operation.

B Brackets first

O Orders (ie Powers and Square Roots, etc.)

DM Division and Multiplication (left-to-right)

AS Addition and Subtraction (left-to-right)

Internal
http://www.mathsisfun.com/operation-order-bodmas.html
An Example
7 + (6 × 52 + 3)
Start inside Brackets, and then use
7 + (6 × 25 + 3)
"Orders" First
7 + (150 + 3) Then Multiply

7 + (153) Then Add

7 + 153 Brackets completed, last operation is add


160 DONE !

Internal
"A C program is like a fast dance on a newly waxed dance
floor by people carrying razors.“

- Waldi Ravens (NetBSD project)

Internal

You might also like