Hi I'm trying to display all the numbers that are more than 15 but are less than 50 in a nested for loop and I'm not quite sure how to go about it, (I picked up C# 2 weeks ago) HELP!

Here's my code

Dani AI

Generated

Clarifying the goal (as asked): list every integer strictly greater than 15 and strictly less than 50 that is divisible by 4 but not divisible by 5. That range is 16..49. was right to point at the modulus operator; had a good start with a loop, and is correct that the exact requirement (inclusive/exclusive bounds, the "not by 5" part) must be clear.

A straightforward C# loop that matches the stated range and both conditions looks like this:

for (int n = 16; n < 50; n++)
{
if ((n % 4) == 0 && (n % 5) != 0)
Console.WriteLine(n);
}

A compact LINQ alternative:

var results = Enumerable.Range(16, 34)
.Where(n => n % 4 == 0 && n % 5 != 0)
.ToList();

Expected output for the stated constraints: 16, 24, 28, 32, 36, 44, 48.

Quick tips and pitfalls:

  • "> 15" means start at 16. "< 50" means stop before 50 (use n < 50), not <=.
  • Use && (logical AND) so the second check is skipped when the first fails; avoid & unless you want both sides evaluated.
  • Parentheses around % checks improve readability.
  • If this was homework practice in nested loops, note a nested loop is unnecessary here; a single loop or LINQ query is clearer and faster.

References: C# remainder operator documentation (remainder operator) and Enumerable.Range (Enumerable.Range).

Recommended Answers

All 3 Replies

First of all decide what your question is. I cant understand, in the title you have something, and then something else. Properly describe, your question, and the answer you have come up with till now.

Two weeks is enough for you to grasp the basic. If you are here looking for someone to do your work, then try somewhere else.

Do some work. if you have errors, we'll help you correct them.

Sounds like another student homework assignment. We get lots of those up here.
Anyway, study the modulus operator, and you will discover the answer.

do u mean that accept division on 4 without remaining??

for(int i=15; i<50; i++)
{
if( i%4 == 0)
Console.WriteLine(i); //prints number 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.