I need help with challenge #6.

Updated 1 day ago in [A] C# .Net
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
3 on February 2, 2018

So I’ve got to the challenge where you have to create a for loop to print out even numbers and I need a little help with it.

 

I had an idea to create a new int called ‘even’ which would determine if the value was even using the equation that Brackey’s gave in the video (x % 2 =0), and then made an if statement checking if even = 0, and if so to print the value in i. But it’s not working and I’m kind of lost. Here’s my code.

 

 

using System;
namespace FirstConsoleProject
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            for (int i = 0; i < 20; i++) {
                int even = Math.Abs(i % 2 = 0); 
                if (even == 0)
                {
                    Console.WriteLine(i);
                }

                else {
                    
                }
            }

            Console.ReadKey();
        }
    }
} 

  • Liked by
Reply
1 on February 2, 2018

 You’re trying to do a comparison inside Mathf.Abs.

Try replacing it with just

int even = i % 2;
on February 2, 2018

Thanks I ended up doing it this way after finding a solution in a youtube comment (of all places).

 

for (int i = 0; i < 20; i++)
     {
          if ( i % 2 == 0) {
                Console.WriteLine(i);
            }
}
Show more replies
  • Liked by
Reply
Cancel