Thread: Is there a command for checking whole numbers?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    62

    Is there a command for checking whole numbers?

    I'm working on some exercises, and not entirely sure how you check for fractional numerics? By this I mean, I need a piece of code or a command that I can use in my IF statement that will let me check if a number i'm dividing is divisible by two numbers. However I do not know how to make it "check" if a decimal is left over.

    Pseudo-coding this. I basically am just wanting

    if 100 divisable by 3
    output
    else
    output

    if 100 divisble by 4
    output
    else
    output


    etc etc etc. It just uses a loop to go up to 101, then 102 etc. All that I can do. But I need the part that will tell me if it is or not. Not sure if there is a built in code or something my mathematically retarded brain doesn't know how to figure out

    (For reference. I don't need the entire thing written out. I just need a small line or two of code that will let me check and determine if it's divisble or not).

    Thanks in advance

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    In C/C++ you can do "integer arithmetic", so when you divide there is no fractional part, e.g. 7 / 2 = 3. So if you're working with integers, it's not appropriate to test for a fractional part.

    Regarding your specific problem, divisibility is generally tested using the '%' operator, which gives you the remainder of a division. x is divisible by y iff there's no remainder when x is divided by y, so you can do x % y == 0 to test whether x is divisible by y.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by Mozza314 View Post
    In C/C++ you can do "integer arithmetic", so when you divide there is no fractional part, e.g. 7 / 2 = 3. So if you're working with integers, it's not appropriate to test for a fractional part.

    Regarding your specific problem, divisibility is generally tested using the '%' operator, which gives you the remainder of a division. x is divisible by y iff there's no remainder when x is divided by y, so you can do x % y == 0 to test whether x is divisible by y.
    Thanks a tonne. So just to make sure I could do

    100 % 3 == 0 would give me 'false' and skip my IF and go to else?

  4. #4
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by NinjaFish View Post
    Thanks a tonne. So just to make sure I could do

    100 % 3 == 0 would give me 'false' and skip my IF and go to else?
    Yep. 100 % 3 is 1 (i.e. 100 divided by 3 is 33 remainder 1).

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    62
    Quote Originally Posted by Mozza314 View Post
    Yep. 100 % 3 is 1 (i.e. 100 divided by 3 is 33 remainder 1).
    Thank you kindly sir. Worked like a charm and my program works flawlessly. Was very simple to figure out how to implement as well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matching numbers
    By kirksson in forum C Programming
    Replies: 7
    Last Post: 07-23-2008, 01:51 PM
  2. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM