Num % 10

This is a discussion on Num % 10 within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me how to use the % function? ok i need to have the last number in a ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    29

    Num % 10

    Can anyone tell me how to use the % function?

    ok i need to have the last number in a decimal number to be put in a nother variable:

    Code:
    num % 10 = A
    will that store the last number in variable "num" (which is an integer) in variable "A"?

    Thanks
    Talitore - Using DEV-C++

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    The % is called the modulus operator. A % B means what is the remainder if A were divided by B.
    Code:
    7 % 5 = 2  // means 7 divided by 5 has a remainder of 2
    10 % 5 = 0 // means 10 divided by 5 has a remainder of 0
    Any integer modulus 10 will tell you what the last digit in the integer is so:
    Code:
    159 % 10 = 9
    2912381 % 10 = 1
    So the answer to your question is yes, it WILL store the last digit into the variable "num".

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    29
    it will store it into variable "num" or "A"
    i asked if it would save it into "A"

    thanks
    Talitore - Using DEV-C++

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    num % 10 = A

    Will give you a syntax error.
    make that:

    A = num % 10

    The last digit of num will be stored in A.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Oops, you're right, I meant it would store it into A, but only if you type it like "A=num%10" like Magos said.

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    29

    Thanks

    Thanks for all your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mkdir - solaris 10
    By StevieT in forum C Programming
    Replies: 4
    Last Post: 08-17-2006, 03:36 AM
  2. Programme Efficiency
    By Cikotic in forum C Programming
    Replies: 3
    Last Post: 06-28-2004, 01:29 PM
  3. how to handle integer overflow in C
    By kate1234 in forum C Programming
    Replies: 8
    Last Post: 04-23-2003, 12:20 PM
  4. Scheduling Algo
    By BigDaddyDrew in forum C++ Programming
    Replies: 41
    Last Post: 03-08-2003, 10:00 AM
  5. preceding zeros in int variables
    By AshFooYoung in forum C Programming
    Replies: 2
    Last Post: 09-23-2001, 02:28 PM

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