C language have a following arithmetic operators. Assume variable i holds 100 and variable j holds 200 then –
Operator | Description | Example |
---|---|---|
+ | Adds two operands. | i + j = 300 |
− | Subtracts second operand from the first. | i − j = -100 |
* | Multiplies two operands. | i * j = 20000 |
/ | Divide. | j / i = 2 |
% | Modulus Operator returns the remainder of integer division. | j % i = 0 |
++ | Increment operator increases the value of variable by one. | i++ = 101 |
— | Decrement operator decreases the value of variable by one. | i– = 99 |
Example : Write a program to demonstrate the various arithmetic operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<stdio.h> int main() { int a=10; int b=25; int c,d ,e,f; c=a+b; // add value of variable a&b and store in variable c (c=35) d=b-a; // subtract value of variable a from b and store in variable d (d=15) e=b/a; // e=2 f=b%a; //f=5 printf(" value of c= %i", c); printf("\n value of d= %i", d); printf("\n value of e= %i", e); printf("\n value of f= %i", f); return 0; } |
OUTPUT
1 2 3 4 | value of c= 35 value of d= 15 value of e= 2 value of f= 5 |
Increment and Decrement Operator
- Increment operator ++
- Decrement operator —
In a C language, Increment operator (++) increases the value of variable by one and Decrement operator (–) decreases the value of variable by one.
There are two form of increment and decrement operator:
- Prefix form
- Postfix form
Prefix Form : In the prefix expression operator appears in the expression before the operands.
Example : ++A
In the prefix form first the value of operand is increment or decrement than the value of operand is used in expression.
Example1 2 | int i = 10; j = ++i ; // j=11 , i=11 |
Postfix Form: In the postfix expression operator appears in the expression after the operands.
Example : A++
In the postfix form first the value of operand is used in expression than value of operand is incremented or decremented.
1 2 | int i = 10; j = i++ ; // j=10, i=11 |
Example: Write a program to demonstrate the increment and decrement operator.
1 2 3 4 5 6 7 8 9 10 11 12 | #include<stdio.h> int main() { int a=10; int b,c; b=++a; // b=11, a=11 c=a++; //c= 11, a=12 printf(" value of a= %i", a); printf("\n value of b= %i", b); printf("\n value of c= %i", c); return 0; } |
OUTPUT
1 2 3 | value of a= 12 value of b= 11 value of c= 11 |