Advertisement
/* Arithmetic Operator Example in C*/
#include<stdio.h>
int main()
{
int x=10, y=10; // local variables
int sum = x + y; //addition of two local int variables
int difference = x-y; //subtraction of two local int variables
int multiplication = x * y; //multiplication of two local int variables
printf("Sum of two int variables = %d \n", sum);
printf("Subtraction of two int variables = %d \n", difference);
printf("Multplication of two int variables = %d \n", multiplication);
return 0;
}
Sum of two int variables = 20
Subtraction of two int variables = 0
Multplication of two int variables = 100
/* Division and Modulus operator in C */
#include<stdio.h>
int main()
{
int i=40;
int j=10;
int quotient = i/j;
int remainder = i%j;
printf("Quotient after dividing 40 by 10 : %d \n", quotient);
printf("Remainder after diviiding 40 by 10 : %d \n", remainder);
int a = 70;
int b = 20;
quotient = a/b;
remainder = a%b;
printf("Quotient after dividing 70 by 20 : %d \n", quotient);
printf("Remainder after dividing 70 by 20 : %d", remainder);
return 0;
}
Quotient after dividing 40 by 10 : 4
Remainder after diviiding 40 by 10 : 0
Quotient after dividing 70 by 20 : 3
Remainder after dividing 70 by 20 : 10
Advertisement
/* Arithmetic expressions with equal and unequal priority operators */
#include<stdio.h>
int main()
{
int result = 3+2-4+2; /* + - operators have equal priorities */
printf("Result of 3+2-1+2 is : %d \n", result);
result = 3+2*4+2; /* + * have unequal priorities */
printf("Result of 3+2*4+2 is : %d \n", result);
result = 3+2*4/2; /* + * / operators have unequal priorities */
printf("Result of 3+2*4/2 is : %d \n", result);
result = 3%2*4/2; /* % * / operators have equal priorities */
printf("Result of 3%2*4/2 is : %d", result);
return 0;
}
Result of 3+2-1+2 is : 3
Result of 3+2*4+2 is : 13
Result of 3+2*4/2 is : 7
Result of 3*4/2 is : 2
#include<stdio.h>
int main()
{
int result = (3+2)*4+2;
printf("Result of (3+2)*4+2 is : %d \n", result);
result = 3%(2*4)/2;
printf("Result of 3%(2*4)/2 is : %d ", result);
return 0;
}
Result of (3+2)*4+2 is : 22
Result of 3(2*4)/2 is : 1
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement