2

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sabotenderizer
  • 187
  • 2
  • 7
  • 13
  • 4
    This is why I like curly braces on everything! – dougajmcdonald Mar 03 '13 at 08:33
  • You don't need to do any exponentiation or multiplication, as all perfect squares are sums of consecutive odd numbers, so you can solve this problem with a simple addition: [Perfect square or not?](https://stackoverflow.com/questions/12862637/perfect-square-or-not) – Cœur Oct 11 '18 at 08:16

7 Answers7

2

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);
}
Johanna Larsson
  • 10,531
  • 6
  • 39
  • 50
1

Try this for your counter loop:

for (counter = 1; counter <= maxValue; counter++)
{
   square = Math.Pow(counter, 2);
   Console.WriteLine("{0}   {1}",  counter, square);
}
G-Mac
  • 1,173
  • 13
  • 10
1

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

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
0

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

Matt
  • 6,787
  • 11
  • 65
  • 112
0

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

exexzian
  • 7,782
  • 6
  • 41
  • 52
0

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.

Srikanth Venugopalan
  • 9,011
  • 3
  • 36
  • 76
0

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"));
        }
    }