-1

I am relatively new to programming and I want to it to loop in a "do while" loop while the quotient of two variables is not a multiple of 4.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Ratatosk1
  • 65
  • 4
  • 1
    `while ((x / y) % 4).nonzero?` – ForeverZer0 Jul 19 '18 at 00:16
  • 2
    @ForeverZer0 What version of C has `.nonzero`? – Barmar Jul 19 '18 at 01:02
  • @Barmar None lol, I had at first thought it was Ruby tag, I corrected obviously in my answer. I would hope that `nonzero?` can still be figured out without explicit and long explanations. – ForeverZer0 Jul 19 '18 at 01:08
  • @ForeverZer0 Yeah, "new to programming" means he probably can't figure out anything. :) Didn't notice you also posted a better answer. – Barmar Jul 19 '18 at 01:12
  • 1
    What are `x` and `y`? Are they always integers? You are just saying "variables". – lurker Jul 19 '18 at 01:16
  • 3
    Do you want to test whether the mathematical quotient *x* / *y* is not a multiple of four or whether the quotient `x / y` computed with integer arithmetic (hence the mathematical quotient truncated to an integer) is not a multiple of four? For example, the mathematical 21/5 is not a multiple of 4, but the computed `21/5` is. – Eric Postpischil Jul 19 '18 at 01:17

3 Answers3

1

The modulo operator, or % will do exactly as you need. It will return the "remainder" of dividing a number by a given value. Using that logic, if the result is 0, then the specified number was divided evenly by the value.

Therefore:

do
{
    // do stuff that changes x and/or y
} while ((x / y) % 4) != 0)

Should accomplish your goals. You divide your x/y value, and then use % 4 on the result. If the result is zero, then the it was evenly divisible by 4, if it is not zero, there was a remainder and it was not evenly divisible.

As pointed out in a comment below, the do...while syntax first "does", then evaluates, which although not indicated in your question, is unlikely the intended behavior, and what you need is simply a while loop without the do. This first evaluates, and then "does" only if the result was true, otherwise does nothing.

while (y != 0 && (x / y) % 4) != 0)
{
    // do stuff that changes x and/or y
}
ForeverZer0
  • 2,379
  • 1
  • 24
  • 32
  • Since this question is tagged as C, you probavly should adjust the syntax of your while loop. – templatetypedef Jul 19 '18 at 00:24
  • I could have swore that it was tagged Ruby when I read it, lol, Fixed it. – ForeverZer0 Jul 19 '18 at 00:26
  • The question language is not clear about the requirements, but note that this will evaluate the modulo at the end, which means the last iteration will operate under the condition of x/y%4 == 0, and then exit. – visibleman Jul 19 '18 at 00:31
  • Yeah, I perhaps should clarify that, I simply made it as the question stated, although it is probably unlikely to be the way it should be. – ForeverZer0 Jul 19 '18 at 00:38
  • @visibleman That loop will only operate with (x/y)%4==0 on the very first iteration, and halt if (x/y)%4 is still zero after that first iteration. That's the _only_ way to run with the condition tue on the _last_ iteration. – Mike Housky Jul 19 '18 at 00:42
  • If the OP is a programming noob, you might want to mention checking for `y` value of zero to avoid `x/y` division by zero. – lurker Jul 19 '18 at 01:21
  • The mathematical quotient of 17/4 is 4.25, yet `int x,y; ... (x / y) % 4` is 0. I suspect for OP's goal,17,4 should fail, not succeed. – chux - Reinstate Monica Jul 19 '18 at 03:37
0

I'll give you the math. Let Z = X / Y. Then you want Z % 4 to be non-zero.

Tal Rofe
  • 457
  • 12
  • 46
-2

try like this.You can use z to store result of (x/y)%4.

main(){
int x=0,y=0, z=0;
  do{ // do anything 

  }
  while((x/y)%4 != 0);
}
bulliedMonster
  • 236
  • 1
  • 8
  • 18
  • someOne downvoted only exact answer written in c? – bulliedMonster Jul 19 '18 at 00:41
  • I don't downvote but I would say it deserves one. There is no return value and no argument declaration in your `main`. I advise that you compile your source code with `-std=c11 -Wall -Wextra`. :) – Paul Jul 19 '18 at 01:08
  • 2
    Your code has no explanation why this is a solution, and it mentions using a variable to store the result but doesn't actually do that. Whether the code is correct or not it isn't going to teach anyone anything. – Retired Ninja Jul 19 '18 at 01:10
  • 1
    I also didn't down-vote, but your answer mentions using z, but then your code does not use z for anything. – visibleman Jul 19 '18 at 01:10
  • Nor did I down-vote. But what if the "do anything" doesn't change `y`? Then `x/y` will be a division by zero. – lurker Jul 19 '18 at 01:20