The condition n % 2 == 0 checks if n is even so the meaning of cout << (n % 2 == 0) ? -n : n; is that if n is even -n will be printed, otherwise n will be printed.
I think it hat to do with the priority of the operators. 1 is the result of (n % 2 == 0) which is true for 4.
If you try with cout << ( (n % 2 == 0) ? -n : n); output will be -4.
> first i also thought this like if and else but output is not same
Tip: in general, and particularly while learning the language,
compile the code with warnings enabled; ideally test the code with more than one compiler.
(Linux: compile with both g++ and clang++.
Visual Studio: also compile the the code with the 'Clang With Microsoft CodeGen' front end).
#include<iostream>
int main()
{
int n;
std::cin >> n;
std::cout << ( n % 2 == 0 ) ? -n : n ; // can something plz explain
// clang++:warning: operator '?:' has lower precedence than '<<'; '<<' will be evaluated first
// clang++: std::cout << ( n % 2 == 0 ) ? -n : n ; // can something plz explain
// clang++: ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^
// clang++: note: place parentheses around the '<<' expression to silence this warning
// clang++: note: place parentheses around the '?:' expression to evaluate it first
// clang++ std::cout << ( n % 2 == 0 ) ? -n : n ; // can something plz explain
// clang++: ^
// clang++: ( )
// g++: warning: second operand of conditional expression has no effect
// g++: std::cout << ( n % 2 == 0 ) ? -n : n ; // can something plz explain
// g++: ^~
// g++: warning: third operand of conditional expression has no effect
// g++: std::cout << ( n % 2 == 0 ) ? -n : n ; // can something plz explain
// g++: ^
std::cout << ( n % 2 == 0 ? -n : n ) ; // this is probably what the programmer intended
std::cout << '\n' ;
}