if (firstnumber % 2 != 0)

If i am correct then the % divides a number and collects its remainder, at this point i want to see wich numbers are odd and wich are even. what does the title mean? % 2 != ?
% is a mod.. it does not return a remainder..

the statement said that if the firstnumber mod by 2 is not equal to 0 then it will go to the first statement
I don't quite get it yet, can you explain it like more carefully and maybe post 1 or two examples.


thanks!


edit: modulus = how many times a number goes into a number and then the rest is the answer?
example 6/2 = 3 because 2*3=6 am i right?
Last edited on
closed account (D80DSL3A)
Your title code will filter for odd numbers. Examples: 3%2 = 1, 4%2 = 0 as % does return the remainder.
A simple test program would reveal this.
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main()
{
	for(int n=1; n<10; ++n)
		if( n%2 != 0 )
			cout << n << " ";
	cout << endl;
	return 0;
}

Output:
1 3 5 7 9
The modulus (%) sign returns the remainder if you say 9%3
Every one takes 3 apples all satisfied 0 remainder which is the result of 9%3
Then if I say 9%2 then the result will be equal to 1 as every one of two will take 4 apples which makes them 8 apples with 1 in the remainder.
Thanks, you answered my question. this thread is now solved!
No problem, anytime.
Topic archived. No new replies allowed.