Asked • 04/04/19

&& operator behaves like || operator?

I am a beginner and I've been trying to run a program that prints all the numbers from 1 to N (user input) except for those that are divisible by 3 and 7 at the same time. What my code does instead, however, is that it prints the numbers from 1 to N except for those that are divisible by 3 or 7. I examined it for a while and I have no idea why it does that. Please explain to me where I'm going wrong. static void Main(string[] args) { int n = 0; int a = 0; n = Convert.ToInt32(Console.ReadLine()); while (a <= n) { a++; if (a % 3 != 0 && a % 7 != 0) { Console.WriteLine(a); } } Console.ReadKey(); } When I reverse the signs of the if statement to `==` the `&&` operator works properly, but if the sign is `!=` it simply acts like an `||` operator, so that confuses me even more. The issue is most likely in the condition, but I can't see what is wrong with it.

1 Expert Answer


Larry C. answered • 04/06/19

Computer Science and Mathematics professional

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.


Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.