By:

Yogendra Pal yogendra@learnbywatch.com Dedicated to My mother and Father

t

Keep your notebook with you.

Write important point and questions that comes in your mind

q

y

Solve Mind band exercise. Rewind when not clear

C

Ask Questions by call or SMS or by mail Keep Watching Keep Learning

THIS IS ARITHMETIC INSTRUCTIONS

2

What do you know? • • • • •

printf() function? scanf() function? How to write a simple C program? How to compile and run it? If answers are yes then you can go ahead.

3

Problem â€˘ Write a program to add two numbers. These numbers must input by user. â€˘ For addition (+) we need arithmetic operators.

4

Arithmetic Operators • • • • • •

* : multiplication / : division + : addition - : subtraction % : modulus operator Variable = constant;

y

z

2

4

2+4 x 6

– x = y + z; 5

Result of each operator • • • • • •

2*3=6 4/2=2 4+2=6 4–2=2 4%3=1 4%2=0

6

Some Constraints • Arithmetic operators act on numeric values. – (int/float/char) operator (int/float/char) – character represents numeric (ASCII) value.

• Modules operator works only when – int % int (non-zero).

• Division operator works only when – (int/float/char) / (non-zero value).

7

You must know • • • •

int / int = int (int / float) = float (float / int) = float (float / float) = float

8

Assignment Operator = • Assign expression value to an identifier. • identifier = expression or variable = constant. • If both operand are of different data types then right side value convert into the data type of left side identifier. • Multiple assignment execute from right to left. – x = y = z = 5.

9

More Assignment Operator… • *=, /=, %=, +=, -= know as compound assignment operator. • x += 1; means x = x + 1; • a -= b; means a = a – b; • m %= (n-7); means m = m % (n-7);

10

Addition of two numbers start

start #include<stdio.h> #include<conio.h>

Define a,b,c

int a,b,c

initialize b,c

b = 4, c = 2

a=b+c

a=b+c

print a

print a

stop

stop 11

void main() { int a, b, c; b = 4; c = 2; a = b + c; printf(â€œ%dâ€?,a); getch(); }

Unary Operators • • • •

Operate upon single operand. - (unary minus) + (unary plus) -- (decrement operator) : decrement by 1 – i--; or --i; equal to i = i -1;

• ++ (increment operator) : increment by 1 – i++; or ++i; equal to i = i +1; 12

Unary Operator… • sizeof : returns the size (bytes) of operand. – printf(“integer: %d”,sizeof(i));

• Type Cast is also a unary operator. • Type Cast changes the data type of operand. – (Type) operand; – float a = 3.7; – (int)a;

13

Operator precedence Operator

Operation

Precendence

()

Parentheses

Evaluate First, Innermost first, Left to right.

-, +, --, ++, sizeof, (type)

Unary operators

Right to left

*, / or %

Multiplication, Division or modulus

Left to right.

+ or -

Addition and subtraction

Left to right.

=

Assignment (Multiple)

Right to left.

14

Example of precedence • • • • • • • • • •

a=2*3/4+4/4+8–2+5/ (3+1) a=2*3/4+4/4+8–2+5/ 4 a=6/4+4/4+8–2+5/ 4 a=1+4/4+8–2+5/ 4 a=1+1+8–2+5/ 4 a=1+1+8–2+1 a=2+8–2+1 a = 10 – 2 + 1 a=8+1 a=9 15

q

Mind Bend

• Calculate the value. – ( 3 * 5.9 – 2 * 6 ) % ( 4 * 8 + 4 ) –2*((8/6)+(4*(9–4))%(3*2–2) – ( 3 * 9 - 3 ) % ( 5 + 3 * 3) / ( 6 - 7 )

16

Calculate the average start

start

Define a,b,c

Define a,b,c

initialize b,c

initialize b,c

a=b+c/2

a=(b+c)/2

print a

print a

stop

stop 17

Problem • Write a program to convert a value from kilometer to meter. • Formula to convert km in to meter is: – 1 km = 1000 m so, – n km = 1000 * n m

• Now you can solve any formula based problem. 18

Problem… • Write a program that convert a lowercase character to uppercase character. • Write a program to calculate the area and circumference of the circle. • Two numbers (x and y) are entered through the keyboard. Write a program to interchange the value of x and y. • Write a program that print reverse of a 5 digit given number. 19

Problem… • Write a program that calculate the root of a quadratic equation. – ax2 + bx + c = 0.

• Input marks of five subjects of a student and write a program to calculate and print addition and percentage of that student.

20

q

Mind Bend

• Write a program that convert a uppercase alphabet to lowercase alphabet. • Write a program to calculate the area and perimeter of rectangle. • Write a program that print reverse of a 3 digit given number.

21

To get complete benefit of this tutorial solve all the quiz on www.learnbywatch.com For any problem in this tutorial mail me at yogendra@learnbywatch.com with the subject “C” For Other information mail at info@learnbywatch.com

Keep Watching Keep Learning

NEXT IS DECISION CONTROL 22

Arithmetic Instructions

Published on Jul 14, 2010

This file is first slide of c programming video book, my first interaction with c programming.