Frist, you need the % remainder operator:
if (x = 16 % 4){ printf("remainder in X"); }
Note: it will not work with float / double, in this case you need to use fmod (double numer, double denom); .
Secondly, to implement it as you wish:
if (x = 16 / 4) , if there is no remainder, x = x - 1 ;If (x = 16 / 5) , then x = x + 1 ;
Using the comma operator , you can do it in one step as follows (read comments):
int main(){ int x = 0, // Quotient. n = 16, // Numerator d = 4; // Denominator // Remainder is not saved if(x = n / d, n % d) // == x = n / d; if(n % d) printf("Remainder not zero, x + 1 = %d", (x + 1)); else printf("Remainder is zero, x - 1 = %d", (x - 1)); return 1; }
Check @codepade's working codes: first , second , third .
Note that in the if-state, I use Comma Operator:, to understand the operator ,: a comma operator with an example .
source share