# How do you check to see if something is a multiple of 4

This is a discussion on How do you check to see if something is a multiple of 4 within the C Programming forums, part of the General Programming Boards category; I think I have to use the modulus operator and an if statement but I'm not sure in which way. ...

1. ## How do you check to see if something is a multiple of 4

I think I have to use the modulus operator and an if statement but I'm not sure in which way. thank you

2. try
Code:
`if x % 4 == 0;`

3. Or, in proper C,
Code:
`if(x &#37; 4 == 0)`
Also see this: http://www.cprogramming.com/tutorial/modulus.html

4. oops. thanks for cleaning me up dwks

5. Originally Posted by trprince
I think I have to use the modulus operator and an if statement but I'm not sure in which way. thank you
Also, the bitwise method:

Code:
```if(x & 3)
{
/* Not a multiple of 4 */
}```
Which is what the compiler will most likely translate the modulo-version into.

6. ## For Loops

Thank you both for answering my quesion in such a timely manner. One other thing I am hving trouble with is using the FOR loop to draw lines or a shape like a square to be simple cause I don't get it at all. I got this wrong on my quiz last week. I tried to draw a square using the character #, 4 across and down. I wrote
Code:
```for(width=1;width<=20;width ++){
for(height=1;height<=20;height++){
prinf("#");
}
}```

thanks.

7. Originally Posted by brewbuck
Also, the bitwise method:

Code:
```if(x & 3)
{
/* Not a multiple of 4 */
}```
Which is what the compiler will most likely translate the modulo-version into.
The bitwise version is only portable if x is unsigned, though (or if you know that the value of x is nonnegative).

8. Originally Posted by trprince
Thank you both for answering my quesion in such a timely manner. One other thing I am hving trouble with is using the FOR loop to draw lines or a shape like a square to be simple cause I don't get it at all. I got this wrong on my quiz last week. I tried to draw a square using the character #, 4 across and down. I wrote
Code:
```for(width=1;width<=20;width ++){
for(height=1;height<=20;height++){
prinf("#");
}
}```

thanks.
4x4 is 16 yes (where'd 20 come from?), but you want to loop through each row (0 to 4) and print each column (0 to 4), so each loop would be from 0 to 4.

Code:
```for(width = 0; width < 4; width++)
{
for(height = 0; height < 4; height++)
{
printf("#");
}
}```

9. And you probably want to print a newline after the inner for loop . . .

Or, of course, you could do something like this -- and it's probably more relevant:
Code:
```for(x = 0; x < 4 * 4; x ++) {
putchar('#');
if(x &#37; 4 == 0) putchar('\n');
}```

10. ok thanks...so my idea was in the right direction but my numbers were wrong. ok so for my exercises that I am working on now I have to create an x within a square using two symbols. my idea is using a for loop again. But will it be two like from above or something diferent since it needs to print on a diagonal? Again I really appreciate your help.

11. ok thanks...so my idea was in the right direction but my numbers were wrong.
Well, that and you'd have gotten a bunch of characters in one row unless you printed a newline like I suggested.

ok so for my exercises that I am working on now I have to create an x within a square using two symbols. my idea is using a for loop again. But will it be two like from above or something diferent since it needs to print on a diagonal? Again I really appreciate your help.
Something like this?
Code:
```#   #
# #
#
# #
#   #```
Hmm . . . the easiest way that I can see to do that is to have two variables, one counting upwards and one counting downwards; and then if the inner loop's control variable equals either of these variables, print a '#' instead of a space.

Of course, there are better ways to do this. For example, about half of the hashes are printed when the inner and outer loop variable values are the same -- and the other ones are printed when inner=max_outer-outer. Then you only need two variables, for the loops themselves.

All of this is assuming that you have two for loops, one inside the other, since it seems to be the easiest way to go about it.

12. yes.. it would look somethinglike that. so I would print the spaces in one loop and the hash marks in another?

13. Well, sure, you could. But that would be unnecessarily complicated.

Think of it this way. Modify this:
Code:
```for(y = 0; y < height; y ++) {
for(x = 0; x < width; x ++) {
putchar(' ');
}

putchar('\n');
}```
so that asterisks are printed sometimes instead of spaces.

If you print a '*' when x==y, you'll get something like this:
Code:
```#
#
#
#
#```
with
Code:
```for(y = 0; y < height; y ++) {
for(x = 0; x < width; x ++) {
if(x == y) putchar('*');
else putchar(' ');
}

putchar('\n');
}```
That's half the battle, at least.

Now, how would you do the other diagonal? Well, it's just the same as the previous one except that it occurs where x=(max_y_value-y) or x=((height-1)-y), since the maximum y gets to is height-1. (Alternatively, it's where y=((width-1)-x).) So, print asterisks on those occasions, too.

14. thank you