Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formula for finding a square of a number

I need to display the squares of the numbers 1-10 using a for loop. This is what I have so far. I don't know what I am missing. Any help would be much appreciated.

        for (int counter = 1; counter <= 10; counter++)
        {                          
            Console.WriteLine(counter * counter);                
        }
        Console.ReadLine();
like image 400
Vinnie Centofanti Avatar asked Oct 17 '25 09:10

Vinnie Centofanti


2 Answers

Have a look at your code

for (int counter = 1; counter <= 10; counter++)
{
   if ((counter * counter) == 0) // this will never evaluate to true
   {
       Console.WriteLine(counter);
   }
}

Since you are starting off with 1 your if condition is never true, so nothing would be printed

you just need to use counter * counter printed in your for loop

or you can use Math.Pow(counter, 2.0) to get your squares

like image 110
V4Vendetta Avatar answered Oct 18 '25 23:10

V4Vendetta


Try this

    for (int counter = 1; counter <= 10; counter++)
    {          
            Console.WriteLine(counter*counter);
    }
like image 37
Tilak Avatar answered Oct 18 '25 23:10

Tilak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!