#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

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.

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.

This article has been dead for over six months. Start a new discussion instead.