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

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    Question 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. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    try
    Code:
    if x % 4 == 0;

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Or, in proper C,
    Code:
    if(x % 4 == 0)
    See this page: http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    Also see this: http://www.cprogramming.com/tutorial/modulus.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    oops. thanks for cleaning me up dwks

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by trprince View Post
    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. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    60

    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. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by brewbuck View Post
    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. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by trprince View Post
    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. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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');
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    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. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    yes.. it would look somethinglike that. so I would print the spaces in one loop and the hash marks in another?

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    60
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 03:13 AM
  2. WM_COPYDATA and mutex selecting multiple files
    By gh0st in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2006, 02:22 PM
  3. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  4. How to check if a multiple of 3?
    By Shanedudddy2 in forum C++ Programming
    Replies: 6
    Last Post: 01-12-2003, 03:10 PM
  5. multiple keypresses
    By DavidP in forum Game Programming
    Replies: 10
    Last Post: 04-07-2002, 06:49 PM