C Tutorial/Operator/Arithmetic Operators
Arithmetic operators in action
- Arithmetic operators: +, -, *, \ and the modulus operator %.
- % cannot be used for float data type or double data type.
<source lang="cpp">#include<stdio.h>
main(){
int a = 1,b = 2,c = 3,d = 4; int sum,sub,mul,rem; float div; 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);
}</source>
Common Arithmetic Operators
Operator Description Example
Multiplication Result = Operand1 * Operand2; / Division Result = Operand1 / Operand2; % Modulus (remainder) Remainder = Operand1 % Operand2; + Addition Result = Operand1 + Operand2; - Subtraction Result = Operand1 -Operand2;
Using the Addition operator
<source lang="cpp">#include <stdio.h> main(){
int iOperand1 = 0; int iOperand2 = 0; int iResult = 0; printf("\nEnter first operand: "); scanf("%d", &iOperand1); printf("Enter second operand: "); scanf("%d", &iOperand2); iResult = iOperand1 + iOperand2; printf("The result is %d\n", iResult);
}</source>
Enter first operand: 1 Enter second operand: 2 The result is 3