Evaluate c in the given java expression if a = 5, b = 8, [2]
int c= ++a + b++ + a+ --b+a++
Answers
Answer:
Arithmetic Operators are the type of operators which take numerical values (either literals or variables) as their operands and return a single numerical value.
Let's assume, 'a' is 8 and 'b' is 4.
Operator Description Example
+ Adds operands a+b=12
- Subtracts second operand from the first a-b=4
* Multiplies both operands a*b=32
/ Divides numerator by denominator. a/b=2
% Modulus Operator returns the remainder after an integer division. a%b=0
#include <stdio.h>
int main()
{
int a = 25;
int b = 8;
printf("sum = %d", (a+b));
printf("\ndifference = %d", (a-b));
printf("\nproduct = %d", (a*b));
printf("\nremainder = %d\n", (a%b));
return 0;
}
Output
Now let's talk about '/'.
If we divide two integers, the result will be an integer.
5/2=2 (Not 2.5)
To get 2.5, at least one of the numerator or denominator must have a decimal(float) value.
5.0/2=2.5 or 5/2.0=2.5 or 5.0/2.0=2.5 but 5/2 = 2.
#include <stdio.h>
int main()
{
int a ;
int b ;
a = 2 ;
b = 9 ;
printf ( "Value of b/a is : %d\n" , b/a ) ;
return 0;
}
Output
#include <stdio.h>
int main()
{
float a ;
int b ;
a = 2.0 ;
b = 9 ;
printf ( "Value of b/a is : %f\n" , b/a ) ;
return 0;
}
Output
As we saw, if 'b' and 'a' are both integers, then the result is 4 (not 4.5) but when one of them is float then the result is 4.500000 (a float).