Listado de la etiqueta: Arithmetic


There are several types of operators are present in C languages. With these operators, we can manipulate different types of operands or data with different procedure. Arithmetic operator is one of the operators by which we can operate different types of mathematical values. First, we have to see that what the position of arithmetic operator in operator groups is.

Operator Groups:

  • Unary operators
  • Arithmetic operators
  • Bitwise operators
  • Relational operators
  • Logical operators
  • Conditional operators
  • Assignment operators

One kind of proceeding rules, if in a problem there are multiple operator present, then this type of problem is solved according to this order of operator groups. This means:

Unary -> Arithmetic -> Bitwise -> So on.

Arithmetic Instruction:

Arithmetic Instruction is an Instruction which is used to manipulate data using operators.

Example:

1

3    +     4     *    5

One result may be 23 another may be 35.

A result is Right, another is wrong. We use operator proceeding BODMAS. But in C language, there is no rule of BODMAS.

Arithmetic operators:

There are different types of arithmetic operators are present in C language. The main operators are division (/), multiplication (*), addition (+) and subtraction (-). Their priorities are as follows:

*, /, % (same priority) 1st priority

+, – (same priority) 2nd priority

Associtivity rule is from Left to Right.

For example:    3 + 4 – 7 = 7 – 7 = 0.

Here two operators are used. One is addition operator and another is subtraction operator. As both operators belong to the same priority level, so preceding rules are followed from left to right. For this addition operator executes first then subtraction operator executes next.

Programming Example 1:

Here we see an example of arithmetic expression. How divide operator is executed in the expression?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include <stdio.h>

int main ()

{

    int  x ;            // declaring a variable.
    x= 3 / 4 ;      // arithmetic expression.
    printf ( «%d», x ) ;

return 0 ;

}

Output:

Explanation:

In this programming example the given arithmetic expression is x= 3 / 4;

In C language, if we perform any operation between two integers, result comes as an integer.  Here 3 and 4 both are integers, so the result has not come up with any actual number. So, it cannot come and float number.

So, 11/5= 2, then result will come 2. If x =3.0/4, the result will come 0.75. It means, if a data type will be actual, then result will come in float.

So,

1
2
3
4
5
6
7

3/4 = 0;

3.0/4 = 0.75;

3/4.0 = 0.75;

3.0/4.0 = 0.75;

It means integer will come if and only if both are integer. Otherwise, it will come any actual number.

% operator gives the result of Remainder.

1
2
3
4
5

x = 23%4 = 3

x= 25%5 = 0

x= 3%4 = 3

If we want to divide any number to another number, means it is divisible or not, then use only modulo (%) operator.

Programming Example 2:

Here we see an example of arithmetic expression. How addition operator is executed in the expression?

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <stdio.h>

int main ()

{

    int  x  ;
    x = 3 + 4 ;
    printf ( «%d n « , x ) ;
    return 0 ;

}

Output:

Explanation:

In this programming example the given expression is x = 3 + 4;

Here two operators are used. One is addition operator and another is assignment operator. As addition operator has the higher priority than assignment operator, addition operator executes first, then assignment operator executes. So the addition of 3 and 4 is 7, then 7 is assigned in the variable x with the help of assignment operator.

Programming Example 3:

Here we see an example of arithmetic expression or how subtraction operator is executed in the expression:

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <stdio.h>

int main ()

{

    int  x ;                // declaring a variable.
    x = 34 ;           // arithmetic expression is used.
    printf ( » %d n «, x ) ;
    return 0 ;

}

Output:

Explanation:

In this programming example the given expression is x = 3 – 4;

Here two operators are used. One is subtraction operator and another is assignment operator. As subtraction operator has the higher priority than assignment operator, subtraction operator executes first, then assignment operator executes. So the subtraction of 3 and 4 is -1, then -1 is assigned in the variable x with the help of assignment operator.

Programming Example 4:

Here we see an example of arithmetic expression. How multiplication operator is executed in the expression?

1
2
3
4
5
6
7
8
9
10
11
12
13

#include <stdio.h>

int main ()

{

    int  x ;            // declaring a variable.
    x = 3 * 4 ;     // arithmetic expression is used.
    printf ( » %d n «, x ) ;
    return 0 ;

}

Output:

Explanation:

In this programming example the given expression is x = 3 * 4;

Here two operators are used. One is multiplication operator and another is assignment operator. As multiplication operator has the higher priority than assignment operator, multiplication operator executes first, then assignment operator executes. So the multiplication of 3 and 4 is 12, then 12 is assigned in the variable x with the help of assignment operator.

Programming Example 5:

Here we see an example of arithmetic expression. How different arithmetic operators are executed in the expression?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include <stdio.h>

int main ()

{

    int  x= 0 ;
    x = 34 + 2115 *3 / 4 ;
    printf ( » The value of expression is: %d n «, x ) ;

return 0 ;

}

Output:

Explanation:

In this programming example the given expression is x = 34 + 21 – 15 *3 / 4;

Here all arithmetic operators are used.  As the addition, subtraction, division and multiplication operators are used in the given expression, higher priority operator executes first, and then other operator executes. So, multiplication and division operator execute first. After that addition and subtraction operators execute as they belong to the less priority level.

Conclusion:

From the above discussion of arithmetic operator in operator groups, we have come to this conclusion that arithmetic operator is one of the most important operator to operator different types of mathematical data. Through arithmetic operator we can easily solve different types of mathematical operation.



Source link