How to check if a multiple of 3?

This is a discussion on How to check if a multiple of 3? within the C++ Programming forums, part of the General Programming Boards category; Yeah well how would I check if a variable was a multiple of 3 thanks Bye Shanedudddy2!...

  1. #1
    Shanedudddy2
    Guest

    How to check if a multiple of 3?

    Yeah well how would I check if a variable was a multiple of 3 thanks
    Bye

    Shanedudddy2!

  2. #2
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    If the number is divisible by three then it is a multiple of 3. Try testing to see if when the number is divided by 3 there is a remainder using the modulus operator.
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main(void)
    {
    	int foobar = 10;
    
    	if ((foobar % 3) == 0)
    		cout << "foobar is a multiple of 3" << endl;
    	else
    		cout << "foobar is not a multiple of 3" << endl;
    
    	return 0;
    }
    Last edited by foniks munkee; 01-11-2003 at 03:44 AM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Sorry, but I just have to say this. This thread seems may too easy
    Code:
    int foobar;
    if(!(foobar % 3))
    	cout << "foobar is a multiple of 3" << endl;
    else
    	cout << "foobar is not a multiple of 3" << endl;
    Is that above solution more complicated than it has to be? Yes. Is that just the way I am? Yes.

    Another way to do this. If a number is divisible by three, the sum of its digits is divisible by three. ex: 121233666995123172990021 is divisible by three, and I know this rather easily by just adding up the digits. You could read in a number as a char array, convert it to an int array, then add up the numbers. You may be thinking "but, then, you'd still have to use the modulus operator!". Not true. If you really wanted a difficult, non-elegant solution, you could do this:
    Code:
    while(foobar >= 0)
    {
    	foobar -= 3;
    }
    if(!foobar)	// if(foobar == 0)
    	cout << "foobar is a multiple of 3!" << endl;
    else
    	cout << "foobar is not a multiple of 3." << endl;
    Can you tell I'm in favor of the contest idea to obfuscate a simple problem as much as possible? Oh my, I am bored. I need to find something better to do. *walks away*
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    LOL. Yea I'd say Josh is a little bored right now.

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Code:
    #include<iostream>
    int main
    (){int
    
    
    foobar;     std::cout<<
    
    "foobar is "
    
    
    <<
    (foobar%3?
       "not ":"")
                <<"a multiple of 3."    
          
           <<       std::endl; return
                
           0;
    }
    pwned

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    For additional speed, you could use a look-up table, i.e. precompute the values.
    Code:
    int main()
    {
      bool* Div3Array = new bool[std::num_limits<int>::max()];
      for (int i=0;i<std::num_limits<int>::max();++i)
        Div3Array[i] = i%3==0; //or another algorithm
      
      //Now we can use the array to determine if an arbitrary
      //number is divisible with 3
      int num;
      cin >> num;
      cout << Div3Array[i];
    }
    This solution might be a little memory-intensive, though.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    #define XX goto
    #define O int
    O main(){O o;cin>>o;x: if(--o<2||o--==--o)
    XX X;XX x;X: cout<<(--o==-2?"yes":"no");
    return 0; }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 04:13 AM
  2. WM_COPYDATA and mutex selecting multiple files
    By gh0st in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2006, 03:22 PM
  3. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 07:31 AM
  4. Replies: 1
    Last Post: 05-01-2003, 03:52 PM
  5. multiple keypresses
    By DavidP in forum Game Programming
    Replies: 10
    Last Post: 04-07-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21