Results 1 to 6 of 6

Thread: 'A' mod 'B' Problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    1

    Smile 'A' mod 'B' Problem

    Hi there ppl. This might be very basic to ask but i have only just started college and i have been given homework in my programming class.

    I have been asked to make a program which completes basic mathematical functions. One of my tasks is to make a code that can answer - ('A' mod 'B').

    I know what the questions means and i would possibly know how to code it, but i do not know the formula for ('A' mod 'B')

    One of my questions is to write 'A' to the power 6 so i wrote - (num1^6) and it works fine.

    By the way, 'A' is the number 12 and 'B' is the number 5. so obviously the answer is 2 but i need a formula for it.

    Any help would be greatly Appreciated

    Thank You

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: 'A' mod 'B' Problem

    A mod B = the remainder of the division A/B = the remainder of the division (A-B)/B assuming A-B is positive. To code it you could use this (crude) identity, or you could also infer another without too much hassle. It just takes a bit of algebra and reasoning. That is, the formula you're looking for is simple enough that giving you any good hint would just give it away completely, and probably ruin the exercise. You could also try to code the algorithm your brain uses--how did you know 12 mod 5 = 2? If you reasoned it well, you can make a computer do the same thing.

    I'm not really sure what you mean about the exponent stuff, most programming languages have powerful exponent operators built in so I guess that's what you should use?
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: 'A' mod 'B' Problem

    in the IDE click on Help search and type in

    mod

    then press enter
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: 'A' mod 'B' Problem

    Hmm, I (and I think jemidiah aswell) was under the impression the poster wants a formula for the Mod operator, instead of actually using the Mod operator... But maybe we're mistaken..?

    If you don't really care how the operation is done you can use VB's inbuilt "Mod" operator:
    Code:
    Result = 12 Mod 5

    If you need to find a different mathematical expression that will allow you to calculate A Mod B without actually using the "Mod" operator you can use this:
    Code:
    Result = A - B * Math.Floor(A/B)

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: 'A' mod 'B' Problem

    First off we need more information. For one thing, we need to know the language. We assume VB, and NickThissen has gone further in assuming VB.NET, but is that correct?

    Also, what is the objective of the assignment? There are at least two VERY simple ways to get the Mod that you ask for (one being the Mod statement and the other being integer division followed by a simple subtraction), but that seems too trivial. Are you trying to replicate manual long division? That would take quite a bit more coding (though only quite a bit because the other solutions are one and two lines, such that a ten line program would be 5-10x as many lines).
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width