Thread: Modulo

  1. #1
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949

    Modulo

    Just wondering.....how does modulo ( % ) work exactly? And if any number % 2 == 0 then the number is even, right? Thanks!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how does modulo ( % ) work exactly?
    Something like this (except far better optimized):
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int lhs = 7;
      int rhs = 3;
      int result = 0;
      int rem = lhs;
    
      for ( result = 0; rem >= rhs; result++ )
        rem -= rhs;
    
      printf ( "%d %% %d == %d\n", lhs, rhs, rem );
      printf ( "%d %% %d == %d\n", lhs, rhs, lhs % rhs );
    
      return 0;
    }
    >And if any number % 2 == 0 then the number is even, right?
    That would be a good assumption.

    -Prelude
    My best code is written with the delete key.

  4. #4
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    thx
    Do not make direct eye contact with me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with modulo calculation
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 03:37 PM
  2. Replies: 7
    Last Post: 06-01-2008, 07:47 AM
  3. modulo 2 modulus
    By breik in forum C Programming
    Replies: 5
    Last Post: 03-24-2006, 02:30 PM
  4. Modulo on decimal numbers
    By (Slith++) in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2005, 03:29 AM
  5. modulo 2 division question help
    By shaq8u in forum C Programming
    Replies: 9
    Last Post: 08-20-2003, 08:37 AM