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
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
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:
n < 50), not <=.&& (logical AND) so the second check is skipped when the first fails; avoid & unless you want both sides evaluated.% checks improve readability.References: C# remainder operator documentation (remainder operator) and Enumerable.Range (Enumerable.Range).
Jump to Post— ChaseVoid 30First 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 …
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
} We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.