How do I skip numbers?

Sep 19, 2018 at 5:48pm
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>

using namespace std;

int main ()

{

for (int i = 1; i <=30; ++i)	{

    
	cout << i << " " ;	


}

return 0;

}
Sep 19, 2018 at 5:56pm
% 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.

1
2
if (i % 6 == 0)
    continue;
Sep 19, 2018 at 6:01pm
What does "related to" mean?
For example, the numbers 12 = 6 * 2, 18 = 6 * 3, and 24 = 6 * 4 are skipped, but 30 = 6 * 5 is not.
Sep 19, 2018 at 6:11pm
Oh, thanks mbozzi, I didn't even notice that he didn't skip 30. I assume it's a typo.
Sep 19, 2018 at 6:17pm
@mbozzi
by 'related to' I mean multiples of 6 and numbers that contain 6 like 16, 26.
Why cant 30 be skipped?


@Ganado
Thank you, that helped a lot.
but how do i skip numbers like 16 and 26?
Sep 19, 2018 at 6:23pm
Why cant 30 be skipped?

In the original post:
Aathy wrote:
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..

This example contains the number 30.

(Yeah, it's probably a typo).
Last edited on Sep 19, 2018 at 9:21pm
Sep 19, 2018 at 6:24pm
how do i skip numbers like 16 and 26

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?
Last edited on Sep 19, 2018 at 6:25pm
Sep 19, 2018 at 9:05pm
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.

Enjoy!
Sep 19, 2018 at 9:32pm
Oh, you're correct, he said any sixes, not just ends with six. Yep, the non-cheating way would be to use a loop while dividing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>
#include <iomanip>
bool contains_six(int n)
{
	while (n > 0)
	{
		if (n % 10 == 6)
			return true;
		n = n / 10;
	}
	return false;
}

int main()
{
	std::cout << std::boolalpha << contains_six(1) << "\n";
	std::cout << std::boolalpha << contains_six(1729) << "\n";
	
	std::cout << std::boolalpha << contains_six(6) << "\n";
	std::cout << std::boolalpha << contains_six(6129) << "\n";
	std::cout << std::boolalpha << contains_six(1726) << "\n";
	std::cout << std::boolalpha << contains_six(1629) << "\n";
}
Last edited on Sep 19, 2018 at 9:37pm
Topic archived. No new replies allowed.