More Related Content Similar to Chapter 3 dti2143 Similar to Chapter 3 dti2143 (20) Chapter 3 dti21432. CONTENTS Arithmetic Operators Assigning Mathematical Operator Combinations Unary Sign Operators Increments And Decrement Operator Conditional Operator Logical Expressions Logical Operator 2 2 CHAPTER 4 3. INTRODUCTION In any programming languages, we need operators to construct statements to do an operation and calculations. In this chapter, you will be introduced to several operators such as: Unary Operators Binary Operators Ternary Operators Operator can be applied to variables or any elements in an expression to produce some kind of computation or action. 3 6. Example 1 #include <stdio.h> #include <conio.h> int main(void) { int num= 20; int val=30; num=val+5; /* num=30+5*/ val=num-val; /*val=35-30*/ num=num+2; /*num=35-5*/ val=30/7; /*val=30/7*/ getch(); return 0; } 6 7. Example 2 #include <stdio.h> #include <conio.h> int main(void) { int num= 20; printf (“%d”, num); int val=30; printf (“%d”, val); num=val+5; printf (“%d”, num); val=num-val; printf (“%d”, val); num=num+2; printf (“%d”, num); val=30/7; printf (“%d”, val); getch(); return 0; } 7 8. Example 3 #include <stdio.h> #include <conio.h> int main(void) { int a,b,c,d,result; a=10; b=20; c=15; d=8; result=a*b/(-c*31%13)*d; printf ("%d",result); getch(); return 0; } 8 10. Example 3 #include <stdio.h> #include <conio.h> int main(void) { int num =5; num -=3; num +=4; printf ("%d", num); getch(); return 0; } 10 11. Example 4 total = total / 2; bonus=bonus+800; balance =balance- sum + tax 11 13. The operators are:Unary Increment operators : ++ ++i (prefix) i++ (postfix) Unary Decrement Operators : -- --i (prefix) i-- (postfix) 12 26. EXAMPLE OF QUESTIONS 1) Given: intval, x = 4, y = 3, z = 10; val =(x+y>=3*z) == (x!=3*z+y); The value of val is: 1 0 10 17 20 27. 2) Given: int val, a = 1, b = 2, c = 3, d = 4; val = d+a%c*b; The value of val is: 6 5 0 1 21 28. 3) int cost = 2; int price = 5; cost += ++price * 2; What is answer for cost? 22 29. 4) Assume count = 21. What is the current value of count at each of these statements? a) count-- =………. b) ++count =………. 23