-4

I'm kinda new at c# programming so take it easy on me.

I couldn't find the answer to my (most likely) simple an stupid question (there's no stupid questions!!) so I post here.

I need to write a program which shows numbers from 1 to 10 that aren't divisble by 2, 3 and 8 using "continue" instruction.

My code:

static void Main(string[] args)
        {
            for (int i = 1; i <= 10; i++)
            {
                if (i % 2 == 0 && i % 3 == 0 && i % 8 == 0)  continue;
                Console.Write("{0} ", i);
            }
            Console.ReadKey();
        }

It doesn't work, tho. The main issue is using &/&& operator. It should return both true and true. Help :(

1 Answers1

1

I need to write a program which shows numbers from 1 to 10 that aren't divisble by 2, 3 and 8 using "continue" instruction.

The minimum number that can be divided by 8 with no remainder is 8. So the only numbers that could qualify are 8, 9 or 10.

if (8 % 2 == 0 && 8 % 3 == 0 && 8 % 8 == 0) // false
if (9 % 2 == 0 && 9 % 3 == 0 && 9 % 8 == 0) // false
if (10 % 2 == 0 && 10 % 3 == 0 && 10 % 8 == 0) // false

None of the numbers 8, 9, 10 can be divided by 2, 3, and 8 with a remainder of 0, so of course all numbers would be printed out, as continue would never be triggered. Are you sure it's "2, 3, AND 8" and not "2, 3, OR 8"?

if ((i % 2 == 0 && i % 3 == 0) || (i % 8 == 0))  continue;
trashr0x
  • 6,457
  • 2
  • 29
  • 39
  • wait, you're right, I've made a mistake while rewriting content of the task lmao, thanks man cause otherwise I would be sitting all night at this! – Reputation Nov 12 '17 at 18:59