Modulus division

This is a discussion on Modulus division within the C Programming forums, part of the General Programming Boards category; I have been doing lots of exercises on the book that I have then I went to encryption. This is ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    40

    Modulus division

    I have been doing lots of exercises on the book that I have then I went to encryption. This is just a very simple encryption on the book. It says that you have to enter only 4 digit number and each digit must be encrypted using the following:

    digit + 7 % 10, swap the first digit to the third digit, swap the 2nd digit to the fourth digit. Provide an option to decrypt the encrypted value as well.

    I wrote down the solution before coding it.

    Ex:

    Code:
    I input 1234
    
    1 + 7  = 8
    2 + 7  = 9
    3 + 7  = 10
    4 + 7  = 11
    
    After getting the sum of the each digit + 7 I did: 
    
    
    8 % 10 = 2   //this should be 2, if not, 0 - but the compiler displays 8
    9 % 10 = 1   //this should be 1, if not 0 - but the compiler displays 9 
    10 % 10 = 0 //expected value
    11 % 10 = 1 //expected value as well

  2. #2
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    The modulo operator gives you the remainder from a division operation. 8 / 10 is 0 with a remainder of 8 which is what 8 % 10 gives you.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> 8 % 10 = 2

    This is a very common mistake, and in fact I made it just the other day! But 8 / 10 == 0 R 8, similarly, 18 / 10 == 1 R 8. Put another way 0 * 10 + 8 == 8.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    40
    I never thought of that, so simple, hehehehe..

  5. #5
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Any number modulo a number greater that itself will return the original number.

    2 % 10 = 2

    8 % 10 = 8

    However, 10 % 8 = 2, which is what I believe you were expecting the answer to be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pseudo code for division
    By svaidya in forum C Programming
    Replies: 14
    Last Post: 05-31-2007, 01:48 PM
  2. Problem with modus division.
    By stillwell in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2004, 11:54 AM
  3. modulo 2 division question help
    By shaq8u in forum C Programming
    Replies: 9
    Last Post: 08-20-2003, 08:37 AM
  4. Modular Division problem
    By Malek in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2003, 06:08 PM
  5. using modulus & weighting factors
    By task in forum C Programming
    Replies: 4
    Last Post: 09-11-2002, 05:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21