Hi Im writing this code listing from 1 to 30 but I want to skip any number that is related to 6.
Example
1 2 3 4 5 7 8 9 10 11 13 14 15 17 19 20 21 22 23 25 27 28 29 30.. If you noticed even numbers such as 16 is skipped. I've been trying to figure this out for a while but Im stuck. So far this is the simple code i came up with for all number ranging from 1 to 30
% is called the "modulo" operator, it's similar to saying "remainder".
For example, 17 % 4 is saying "What is the remainder left when you divide 17 by 4". The answer being 1. 16 % 4 == 0, meaning that 16 is perfectly divisible by 4.
Likewise, if you want to skip all numbers that are multiples of 6, that means you want to skip any number that is divisible by 6.
In base 10, if a number's last digit is a 6, that means its remainder, when divided by 10, is 6.
Based on what I wrote earlier about remainders, can you figure out how to code this?
For 16, etc. I'm going to go with the “cheat” method: convert the number to a string and look to see if it has a '6' in it.
1 2 3 4 5 6 7 8 9
// Skip multiples of 6
if (i % 6 == 0) continue;
// Skip numbers that have 6 as any digit
auto s = std::to_string( i );
if (s.find( '6' ) != s.npos) continue;
// Print everything else
std::cout << s << " ";
The other method would be to decompose the number using division and remainder, looking for sixes, as Ganado suggests.