#include <iostream.h>
main()
{
int n, k = 5;
n = (100 % k ? k + 1 : k - 1);
cout << "n = " << n << " k = " << k << endl;
return 0;
}
How the value of n become "4" ?
n=4
k=5
#include <iostream.h>
main()
{
int n, k = 5;
n = (100 % k ? k + 1 : k - 1);
cout << "n = " << n << " k = " << k << endl;
return 0;
}
How the value of n become "4" ?
n=4
k=5
Jump to PostLook at modulo (%) operator and conditional operator.
Conditional operator execute k+1 if an expression 100%k evalutes non-zero. A result of an expression 100%k is 0 (zero) so an expression k-1 will be executed.
Jump to Post#include <iostream.h> main() { int n, k = 5; n = (100 % k ? k + 1 : k - 1); cout << "n = " << n << " k = " << k << endl; return 0; }
How the value of n …
Look at modulo (%) operator and conditional operator.
Conditional operator execute k+1 if an expression 100%k evalutes non-zero. A result of an expression 100%k is 0 (zero) so an expression k-1 will be executed.
#include <iostream.h> main() { int n, k = 5; n = (100 % k ? k + 1 : k - 1); cout << "n = " << n << " k = " << k << endl; return 0; }
How the value of n become "4" ?
n=4
k=5
Hey your code line 1 says
n = (100 % k ? k + 1 : k - 1);
here the right hand side is a conditional statement which has format
(<condition>) ? (<if result is not 0 execute this>) : (<else execute this>);
So here it checks what is 100 % k,which is 100 % 5 here which is 0 so it needs to execute the second statement, and second statement is k -1 so here 5 -1 that is 4 gets assigned to n.
Hey sorry adatapost !!! Slow loading problem.
n = (100 % k ? k + 1 : k - 1);
is the same as
if (100 % k != 0)
n = k + 1;
else
n = k - 1;
The operator %(int a, int b) returns the remainder of the integer division of a and b. See also http://en.wikipedia.org/wiki/Modulo_operator
Oh Thank you very much all of U
i got it now
what delicious C++ is !
Oh Thank you very much all of U
i got it now
what delicious C++ is !
Thanks. Mark this thread as a Solved if you got solution.
We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.