Introduction
You can process data using arithmetic operators such as +, -, *, \ and the modulus operator %. % indicates the remainder after integer division; % cannot be used for float data type or double data type. If both operands i1 and i2 are integers, the expression i1/i2 provides integer division, even if the target is a floating point variable. The operators have normal precedence rules, as follows:
-
Unary operators such as −, + are evaluated.
-
The multiplication (*) and division (/,%) operators are evaluated.
-
The addition (+) and subtraction (−) operators are evaluated.
-
The assignment operator is evaluated.
-
The expressions are evaluated from left to right for unary operators. The assignment is from right to left.
Program
#includemain( ) { int a,b,c,d; int sum,sub,mul,rem; float div; printf("ENTER VALUES OF b, c, d"); scanf("%d%d%d",&b&c,&d); sum = b+c; sub = b-c; mul = b*c; div = b/c; rem = b%d; a = b/c * d; printf("\n sum = %d, sub = %d, mul = %d, div = %f",sum,sub,mul,div); printf("\n remainder of division of b & d is %d",rem); printf("\n a = %d",a); }
Input
b = 10, c = 5, d= 3.
Output
ENTER VALUES OF b, c, d 10 5 3 sum = 15, sub = 5, mul = 50, div = 2.0 remainder of division of b & d is 1 a = 6
Explanation
-
Suppose you have the expression
a = b/c * d
Here / and * both have the same priority. b/c first is evaluated because the expression is evaluated from left to right.
-
After evaluating the expression b/c * d, the value is assigned to a because the assignment operator has an order of evaluation from right to left, that is, the right expression is evaluated first.
No comments:
Post a Comment