Arithmetic operators

Arithmetic operators are:

  1. * (Multiply), /(divide), %(Modulas)-It's priority is most
  2. + -         It's priority is least.

i) Multiplication Operator:
  • This operator multiplies two operands.
  • If in any expression if there is * (multiplication) and + (Addition). Then according to precedence rule * (multiplication) is first solved.
  • If in arithmetic operators, operators are the same then it is solved from left to right.
  • i.e its associativity is left to right.

Program

#include <stdio.h>
#include<conio.h>
int main()
{
    int a;
    a=2*3;
    printf("Answer of multiplication operator is %d",a);
    getch();
}
Output:
Answer of multiplication operator is 6


ii) Integer divide Operator:
Examples:

 2/3 ? = 0
Instead of 0.6.
13/2 =6
instead of 6.5.

Because in c language if any operations are performed between two integers. then, our result will be in integer.

Program:

#include <stdio.h>
#include<conio.h>
int main()
{
    int a;
    a=2/3;
    printf("Answer of Integer divide operator is %d",a);
    getch();
}
Output:
Answer of Integer divide operator is 0



iii) Modulo Operator:

The remainder obtained after the division operation on two operands is known as modulo operation.
The operator for doing modulus operation is "%".
Ex. a%b=c

where c is the remainder obtained when a is divided by b.

Example:


 2%3 ? = 2
Instead of 0.6/0
13%2 = 1
instead of 6.5.

Program:

#include <stdio.h>
#include<conio.h>
int main()
{
    int a;
    a=2%3;
    printf("Answer of modulo operator is %d",a);
    getch();
}
Output:
Answer of modulo operator is 2

iv) Addition and subtraction operator:


4+1-2
It is solved from left to right and ans. is 3.
Here + is operated first.


4-2+1
it is solved from left to right and ans. is 3.
Here - is operated first.

Program

#include <stdio.h>
#include<conio.h>
int main()
{
    int a;
    a=4-2+1;
    printf("Answer of Addition and subtraction operator is %d",a);
    getch();
}
Output:
Answer of Addition and subtraction operator is 3

Comments

Popular posts from this blog

Getting input in C

Unary Operator