blogger Indonesia

Follow judhyns blog

ARITHMETIC OPERATOR

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:

  1. Unary operators such as , + are evaluated.

  2. The multiplication (*) and division (/,%) operators are evaluated.

  3. The addition (+) and subtraction () operators are evaluated.

  4. The assignment operator is evaluated.

  5. The expressions are evaluated from left to right for unary operators. The assignment is from right to left.

Program

 #include  main( ) {   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

  1. 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.

  2. 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:

feed

PR Check

activesearchresults

judhyn's blog

 

http://www.judhyn.blogspot.com | |