Question

I want to make a list of numbers and their squares in C# using a for loop.

Right now I have:

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {

            int counter;
            int square = 1;
            const int maxValue = 10;


            Console.WriteLine("Number  Square");
            Console.WriteLine("-------------------");
            {
                for (counter = 1; counter <= maxValue; counter++)
                    square = counter ^ 2;
                Console.WriteLine("{0}   {1}",  counter, square);
            }

         }
      }

   }

But my output is just an 11 and an 8.

When I put "square = counter ^ 2" right under the variable declarations, I end up with a column of numbers 1-10 but the second row is just a bunch of threes, if they are set to 0 they are twos. It also gives me an error to declare the counter variable if I don't set it to something.

When I put the equation in the position it is now, it asks for the square variable to be declared as something(in here it is at 1).

Also I am a beginner I haven't learned about classes yet, so I'd prefer any corrections not to include them.

EDIT: fixed, gosh I didn't make this mistake last time, yeah I need more practice. Sorry

Was it helpful?

Solution 2

The placement of braces is important:

 Console.WriteLine("Number  Square");
 Console.WriteLine("-------------------");

 for (counter = 1; counter <= maxValue; counter++)
 {
     square = counter * counter;
     Console.WriteLine("{0}   {1}",  counter, square);
 }

Note: It's good practice to always use enclosing braces for for loops and if statements precisely for this reason.

Also note that ^ is not 'to the power of' but exclusive OR

OTHER TIPS

You are accidentally using a shorthand for declaring the for-loop block.

The for statement should be followed by curly braces to indicate the block to be executed. However, if you skip the braces it will simply grab the "next line". In your case, only square = counter ^ 2; is executed in the loop. However, the ^ operator is for the xor operation, not pow.

You want this instead:

Console.WriteLine("Number  Square");
Console.WriteLine("-------------------");

for (counter = 1; counter <= maxValue; counter++)
{
    square = counter * counter;
    Console.WriteLine("{0}   {1}",  counter, square);
}

Try this for your counter loop:

for (counter = 1; counter <= maxValue; counter++)
{
   square = Math.Pow(counter, 2);
   Console.WriteLine("{0}   {1}",  counter, square);
}

The ^ operator is not for that purpose. Use System.Math.Pow() instead. Example: var square = Math.Pow(3, 2). This will give 9.

Your for loop is in short-hand mode. Your console.writeline is outside the for loop.

Try replacing the lines with this

for (counter = 1; counter <= maxValue; counter++)
{
  square = counter * counter;
  Console.WriteLine("{0}   {1}",  counter, square);
}

Note that ^ is not a power operator in C#. It is used for XOR.

square = counter ^ 2 ?? here ^ is an xor operation

do this:
square = counter * counter;

and enclose

{
    square = counter * counter;
    Console.WriteLine("{0}   {1}",  counter, square);
}

inside for - loop

or better use Math.pow method

I would use:

    private void sqtBtn_Click(object sender, EventArgs e)
    {
        outputList.Items.Clear();

        int itemValue, sqt;

        for (int i = 0; i < randomNumAmount; i++)
        {
            int.TryParse(randomList.Items[i].ToString(), out itemValue);

            outputList.Items.Add(Math.Sqrt(itemValue).ToString("f"));
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top