Thread: C++ math operators (the remainder one)

  1. #1
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116

    C++ math operators (the remainder one)

    hey can someone tell me how to use the operator that tells you the remainder whn you divided a number, i think its % and you do like 15%4 and that would give you 3. is this right? also which library do i need for it? thanks in advance.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >> is this right
    Why don't you just try it and see.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    i agree with Hammer. but you do not need a special libary for %. you do need cmath though if you are planning to % doubles.

  4. #4
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    ok i tried it and i think it worked... there was no error or anything, but it didnt give the result i wanted. i am making a program to test for primeness (is that a word?) in a number. here is what i have, but it just says every number is prime:

    also in my for loop i am changing the middle part (forgot what it was called) to be the square root of N instead of half of N, but bare with for now.

    Code:
    #include <iostream>
    
    int main()
    {
      int i = 2; //the first number it tests
      int n;
      int b;
       while(1)
      {
        cout <<"Enter a number to test.\n";
        cin >>n;
        b = (n % i); //be is the remainder of their number and 2
        if(b = 0)    //if there is no remainder...
        {
          cout <<n <<" is not prime. It is divisible by 2."; //...its not prime            
        }
        for(i = 3; i<(n / 2); i = i + 2) //if its odd, it starts with 3, and goes up by 2 each time
        {
          b = (n % i); //same as before, except instead of 2 its the next odd value
          if(b = 0)   //if no remainder...
          {
            cout <<n <<" is not prime. It is divisible by " << i <<".\n\n";  //...then its not prime
          }
        }
        cout <<n <<" is prime.\n\n"; //once i > .5n (at which point its redundent) it declares it prime
      }
    }
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

  6. #6
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    ok thanks rod the % makes a lot more sense now, but i dont understand what i did wrong in my program... why does it aways come up as prime, even if you put in 4 or 12?
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Thumbs up Looks like a good attempt... A couple of things I noticed:

    You need some "else" statements to go with those "if" statements. Or maybe "break;"

    Your code will print the "is prime" message whether it prints "not prime" or not.

    Also, the loop keeps counting up after it discovers "not prime".

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    also, in your if statements, it needs to be:

    Code:
    if(b == 0)
    not
    Code:
    if(b = 0)

  9. #9
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    oh duh, thx alpha, my stupidity never ceases to amaze me
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    no problem, I've done it too. Accidental typos here and there. Happens to everyone.

  11. #11
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    ive put in all the corrections people have made (or so i believe), yet still every number is a prime. here is my latest attemp:

    Code:
    #include <iostream>
    #include <cmath>
    
    int main()
    {
      int i = 2;                           //the first number it tests
      int n;
      int b;
      while(1)
      {
        cout <<"Enter a number to test.\n";
        cin >>n;
        b = (n % i);                       //be is the remainder of their number and 2
        if(b == 0)                         //if there is no remainder...
        {
          cout <<n <<" is not prime. It is divisible by 2."; //...its not prime
        }
        else
        {
          for(i = 3; i < sqrt(n); i = i + 2) //if its odd, it starts with 3, and goes up by 2 each time
          {
            b = (n % i);                     //same as before, except instead of 2 its the next odd value
            cout <<i <<"\n";            //show the last attempt, no reason, just for fun i guess
            if(b == 0)                       //if no remainder...
            {
              cout <<n <<" is not prime. It is divisible by " << i <<".\n\n";  //...then its not prime
            }   //end if
            else                             //if its not yet known, it just repeats the loop
            {
            }   //end else
          }     //end for
          cout <<n <<" is prime.\n\n";       //once i > .5n (at which point its redundent) it declares it prime
        }       //end else
      }         //end while
    }           //end main
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  12. #12
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    try

    Code:
    if(b = n % i)                       
    {
    }
    else
    {
    }

  13. #13
    Geo Geo Geo-Fry
    Join Date
    Feb 2003
    Posts
    116
    i tried your suggestion rod, and it didnt seem to make any difference, and i didnt really understand what you were getting at when you said that... if i say
    Code:
    b = n % i
    than wouldnt it be pointless to have
    Code:
    if(b == n % i)
    {
    }
    thats like saying int i = 2, if i == 2..... you get the point
    however, i do appreciate your trying to help me, and what you said probably works and i just misunderstood it.
    Last edited by Geo-Fry; 03-28-2003 at 01:52 PM.
    "You can lead a man to Congress, but you can't make him think."
    "The Grand Old Duke of York
    -He had ten thousand men.
    -His case comes up next week."
    "Roses are red, violets are blue, I'm schizophrenic, and so am I."
    "A computer once beat me at chess, but it was no match for me at kick boxing."
    "More and more of our imports are coming from overseas."
    --George W. Bush
    "If it weren't for electricity, we'd all be wacthing TV by candlelight."
    --George W. Bush

  14. #14
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    nm i read your code wrong, my mistake.

  15. #15
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Use of else:
    Code:
    if(x)
    {
       // do this
    }
    else
    {
       // Only do this if not x
    }
    Code:
    if(x)
    {
       // do this only if x is not zero (x=true)
    }
    else
    {
       // Only do this only if x is zero (x=false)
    }
    
    {
      // ALWAYS do this...
    
    }
    THIS WORKS FOR ME:
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;				// *** ADDED BY DOUG (reqd for MSVC++)
    
    int main()
    {
      int i = 2;                           //the first number it tests
      int n;
      int b;
      while(1)
      {
        cout <<"Enter a number to test.\n";
        cin >>n;
        b = (n % i);                       //be is the remainder of their number and 2
        if(b == 0)                         //if there is no remainder...
        {
          cout <<n <<" is not prime. It is divisible by 2."; //...its not prime
    	  return 0;							// *** ADDED BY DOUG	    
    	}
    
        else
        {
          for(i = 3; i < sqrt(n); i = i + 2) //if its odd, it starts with 3, and goes up by 2 each time
          {
            b = (n % i);                     //same as before, except instead of 2 its the next odd value
            cout <<i <<"\n";            //show the last attempt, no reason, just for fun i guess
            if(b == 0)                       //if no remainder...
            {
              cout <<n <<" is not prime. It is divisible by " << i <<".\n\n";  // ***MOVED BY DOUG
    		  return 0;						// *** ADDEDE BY DOUG	
    		}   //end if
            else                             //if its not yet known, it just repeats the loop
            {
              cout <<n <<" is prime.\n\n";       //once i > .5n (at which point its redundent) it declares it prime
    		  return 0;						// *** ADDED BY DOUG	
    		}   //end else
          }     //end for
          
        }       //end else
      }         //end while
    return 0;								// *** ADDED BY DOUG
    }           //end main

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. how to use operator+() in this code?
    By barlas in forum C++ Programming
    Replies: 10
    Last Post: 07-09-2005, 07:22 PM
  4. Help with C++ Math
    By aonic in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2005, 04:40 AM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM