Public Group

Archived

This topic is now archived and is closed to further replies.

modulus operator

This topic is 5402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I''m trying to make a program that prints all the numbers between one and ten that are divisible by two. I imagine this can be accomplished using the modulus operator? any help with my code would be appreciated. // All numbers divisible by 2 up until ten #include <math.h> #include <iostream.h> main() { for (int i = 0; i <=10; i++) { cout<<10 % i; } return 0; }

Share on other sites
If you want numbers that are divisible by 2, then the number 2 should be involved somewhere close, oh so very close, to the modulus operator, don't you think ? After all, what is the definition of "x is divisible by y" ? What is the definition of the modulus operator, what is "x % y" ?

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

[edited by - Fruny on February 27, 2004 4:56:53 PM]

Share on other sites
Hint: Divisible by 2

[edited by - PatrickD on February 27, 2004 5:00:04 PM]

Share on other sites
hmmm, ya think i'd remeber to put 2 in the equation???? yeah....I have a tendency to just assume the computer know's what I want it to do :-) i meant to say that i don't want a remainder, just what's evenly divisible by two, my math terminology is failing me... thanks for the fast replies, it's greatly appreciated.

[edited by - jotaka on February 27, 2004 5:07:42 PM]

Share on other sites
This is just a brain dump of sorts:
1. The concept of divisibility can be inverted to yield "multiple." ie, you can achieve the same result by simply computing the multiples of your target variable (2 in this case) that are less than your target limit (10 in this case). Unless you''re just fooling around with the modulus operator.

2. For large collections of divisibility (such as the computation of primes), using methods similar to the Sieve of Eratosthenes is better: Create an indexable bitset (std::bitset or std::bitvector) and zero out indexes that are multiples of your base value. In the case of primes you can stop once you encounter an already zeroed multiple, since all subsequent multiples will have been zeroed as well. Then for whatever limit you have, iterate through your bitset and print the indexes (bitplaces) whose values are non-zero.

3. In the case of your program, what it currently does is print the remaind of dividing 10 by all numbers less than it (unfortunately, including zero - which yields a software interrupt). What you want is to print the values up to 10 whose remainder is zero; to get the quotient you''ll want to employ C integer division.

Hope that helped.

Share on other sites
quote:
Original post by JotaKa
i meant to say that i don''t want a remainder, just what''s evenly divisible by two, my math terminology is failing me...
Quotient.
for( int n = 1; n <= 10; ++n )  if( n % 2 == 0 ) cout << n << endl;

Share on other sites
Divisible by two?
for (int i=0; i<100; i++) if (!(i&1)) printf("%d\n", i);

EDIT: please note 0 is not divisible by two as 0 is not in 1, 2, 3, ... (N);
for (int i=1; i<100; i++) if (!(i&1)) printf("%d\n", i);

[edited by - Kurioes on February 27, 2004 5:42:20 PM]

Share on other sites
quote:
Original post by Kurioes
EDIT: please note 0 is not divisible by two as 0 is not in 1, 2, 3, ... (N);

Zero is divisible by 2.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)

Share on other sites
thanks for all the replies everyone, I was having a brainfart, logic centers of the brain rendered inoperable momentarily. Shortly after i created the intended program. I was trying to implement the modulus operator, however I do like the various other ways of coming to my desired solution so thanks again everyone for the help

Share on other sites
O.K. Think about even and odd numbers: 1)Even numbers yield a particular number when % by a CERTAIN number.2)Odd numbers yield a particular number (which is different from the even number''s result) when % by the same CERTAIN number above.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633701
• Total Posts
3013450
×