Thread: Help writing a function that tests if a number is a multiple of another.

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    9

    Question Help writing a function that tests if a number is a multiple of another.

    How would I write a function that determines if a number is a multiple of another number.
    ex. (is 147 a multiple of 7?)

  2. #2
    Registered User
    Join Date
    Jun 2010
    Posts
    24
    Use the modulus operator (%) to calculate the remainder. If a % b is zero then a is a multiple of b.
    Last edited by Golf7; 10-10-2013 at 05:04 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    and, if you don't want to use the modulus operator (which is sometimes the case in homework exercises of this nature) use division, multiplication, and subtraction.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User Suhasa010's Avatar
    Join Date
    Sep 2013
    Posts
    10

    Alternative for modulus

    You can use this for the present task:
    Code:
    int quotient,number1,number2; quotient=number1/number2; if(( quotient*number2 ) == number1)           printf("it is a multiple");else       printf("it is not a multiple");
    Last edited by Suhasa010; 10-13-2013 at 11:24 AM.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by Suhasa010 View Post
    You can use this for the present task:
    Code:
    int quotient;
    quotient=number/2;
    if(( quotient*2 ) == number)
           printf("Even");
    else
           printf("odd");
    That is the worst way I've ever seen to check for even or odd. The task can be accomplished with this simple comparison:
    Code:
    if (number & 1) { /* odd */ } else { /* even */ }

  6. #6
    Registered User Suhasa010's Avatar
    Join Date
    Sep 2013
    Posts
    10
    Quote Originally Posted by nonpuz View Post
    That is the worst way I've ever seen to check for even or odd. The task can be accomplished with this simple comparison:
    Code:
    if (number & 1) { /* odd */ } else { /* even */ }
    sorry for that, actually it was a blunder while porting the logic to a different scenario, problem was with the divisibility....And yeah, that was a simple one, sorry again for making it complex

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-07-2013, 03:28 PM
  2. Replies: 5
    Last Post: 11-23-2011, 10:24 PM
  3. Help writing to multiple files please?
    By AShaw in forum C Programming
    Replies: 4
    Last Post: 07-07-2011, 10:51 AM
  4. Replies: 2
    Last Post: 05-10-2006, 01:43 PM
  5. Program that averages specified number of tests
    By Fatal in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2005, 03:18 PM