# Thread: what does mod (%) do?

1. ## what does mod (%) do?

I know that this is a stupid question, but I was asleep in class when the teacher explaned it and my book doesn't give a clear cut answer. So can anyone help me?

2. "a % b" will give you the remainder when a is divided by b. Mod is very useful, especially when you want to figure out whether or not a number is even.

3. It gives you the remainder in an equation... 4/2=2, and 1%2 is the remainder of 1/2

4. ohhhhhhh. so thats all it does?

5. Originally posted by Neoground1
ohhhhhhh. so thats all it does?
Yep. You'd use in a way like this:
Code:
```int number;
cin >> number;

if( (number % 2) = 0)
{
// number even
}
else
{
// number not even
}```

6. I was also wondering if there is a way to skip a line of data when you are reading it from a file. This is what im tring to do. I am reading in data from a file. The file has 4 lines of data (40 letter grades and the names of the student after the data) and im trying to skip to the next line after i read 30 charactors. How can i do that?

7. ## % and / operators

Hmm.. for positive integers it's working really, but what if some of given operands is negative? I think that then % operator returns the value, for which this is true: (a/b)*b + (a%b) = a. (maybe I should have said that the equality holds, but I'm not good in english.. what do you think?)
But I'm not quite sure what is a/b doing when one of its operands is less than 0..

8. mazo,

Code:
```#include <iostream>
#include <cstdlib>
#include <cmath>

inline void pause() { system("PAUSE"); }

int main(void)
{
int a, b, c;

a = 35;
b = -7;

c = (a/b)*b + (a%b);

std::cout << "a = " << a << std::endl << std::endl;
std::cout << "b = " << b << std::endl << std::endl;
std::cout << "(a/b) = " << (a/b) << std::endl << std::endl;
std::cout << "(a/b)*b = " << (a/b)*b << std::endl << std::endl;
std::cout << "(a%b) = " << (a%b) << std::endl << std::endl;
std::cout << "(a/b)*b + (a%b) = " << c << std::endl << std::endl;

pause();
return 0;
}```
Substitute what you like for 'a' and 'b', but your equation works.

By the way, your English is quite good. My Slovak is very, very poor. In fact, I know only one word: mazo.

-Skipper

9. Originally posted by face_master
Yep. You'd use in a way like this:
Code:
```int number;
cin >> number;

if( (number % 2) = 0)
{
// number even
}
else
{
// number not even
}```
Face Master... use == 0 instead of = 0

10. Originally posted by skipper
mazo,

Code:
```#include <iostream>
#include <cstdlib>
#include <cmath>

inline void pause() { system("PAUSE"); }

int main(void)
{
//code
std::cout << "a = " << a << std::endl << std::endl;
//more code```

-Skipper
Not absolutely sure... but shouldn't it have using namespace std;?

11. Note, the % operator can be used only with integers. If you want to perform the same operation you will have to use fmod or modf, both functions defined in math.h.

Compiler:Turbo C++ 1.01

12. Not absolutely sure... but shouldn't it have using namespace std;?
No, that's the lazy man's way of doing things. The true programmer will do things the hard way and type out std:: in front of everything, instead of using the new-fangled dad-blasted "using namespace std;"

13. >> Face Master... use == 0 instead of = 0

Silly me...always do that when postin fast

14. No, that's the lazy man's way of doing things. The true programmer will do things the hard way and type out std:: in front of everything, instead of using the new-fangled dad-blasted "using namespace std;"
Thanks, Hunter2...I think.

Actually, given that even Mr. Stroustrup believes that "using namespace std;" is a "pollution" of the global namespace, I tend to use std:: as much as is feasible.

I also try to emulate (note the word "try") the methods of the experienced and knowledgeable programmers on the Board, as well.

Probably a good idea to research the topic and decide for yourself.

-Skipper

15. even Mr. Stroustrup believes that "using namespace std;" is a "pollution" of the global namespace
Really? I thought that was just me, since everyone else uses it

Popular pages Recent additions