Help please.

This is a discussion on Help please. within the C++ Programming forums, part of the General Programming Boards category; I have this project from school that has me program a code to tell the user if a given number ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    12

    Help please.

    I have this project from school that has me program a code to tell the user if a given number is lucky or not. The number is lucky if it is a factor of 7, 11, or ends with a 7. I can do the first two parts but the last i dont get (ends in 7 part). Any help would be awsome.

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try using the % operator.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    convert it to a string and check the last character.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    The % (modulus) operator is cleaner. Or more efficient anyway.
    Code:
    if(number % 10 == 7) {
        std::cout << "number ends with a 7.\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.

Popular pages Recent additions subscribe to a feed

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