Write the output of the given snippet :- if int a=5; c = ++a + a++ % ++ a ; System.out.print(c) ;
Answers
Given códe:-
int a=5
c = ++a + a++ % ++ a ;
System.out.print(c) ;
To find:-
- Value of c
Calculation :-
c = ++a + a++ % ++a ;
= 6+ a++ % ++a; //++a is Prefix increment, a increases to 6
=6+ 6 % ++a; //a++ is postfix increment, here it will continue with previous value and then increases to 7
//Now a is 7
= 6+ 6 % 8; //++a is prefix increment a increases to 8
= 6+6 //6 divided by 8, remainder 6
= 12
Final value :-
- c=12
Always remember
- prefix increment first increases the value and then moves forward
- ++a and --a are prefix increment
- postfix increment continues with previous value and then moves forward
- a++ and a-- are postfix increment.
Ânswer
int a=5
c = ++a + a++ % ++ a ;
System.out.print(c) ;
To find:-
Value of c
Calculation :-
c = ++a + a++ % ++a ;
= 6+ a++ % ++a; //++a is Prefix increment, a increases to 6
=6+ 6 % ++a; //a++ is postfix increment, here it will continue with previous value and then increases to 7
//Now a is 7
= 6+ 6 % 8; //++a is prefix increment a increases to 8
= 6+6 //6 divided by 8, remainder 6
= 12
Final value :-
c=12
Always remember
prefix increment first increases the value and then moves forward
++a and --a are prefix increment
postfix increment continues with previous value and then moves forward
a++ and a-- are postfix increment.