Thread: Determining if a number has a remainder?

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    Determining if a number has a remainder?

    How could I do this in C? Knowing the remainder would be cool too.

    If no one can think of a way of this, how about how can I find out if a number is even or even prime?

    :-/

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    in c/c++ there is a modulus operator %

    this does an integer division and returns the remainder.

    I think you can work the rest out!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest
    Yes, % will work, but here's a fun way:
    Code:
    int divide( int toDivide, int divideBy )
    {
       int x=0;
       x = toDivide / divideBy;
       return ( toDivide == (divideBy * x) );
    }
    That should do something like:
    Code:
    printf("6 / 3 = 2, is there a remainder? %s.", divide( 6,3 ) ? "No" : "Yes" );
    Alternatly, you could just make that return the remainder:
    Code:
    int divide( int toDivide, int divideBy )
    {
       int x=0;
       x = toDivide / divideBy;
       return (toDivide-(x*divideBy));
    }
    Quzah.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by Stoned_Coder
    in c/c++ there is a modulus operator %

    this does an integer division and returns the remainder.

    I think you can work the rest out!
    Hey thanks I had forgotten about the modulus operator!

    I need to refresh my C skills.. hehe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  4. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM