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();
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
Try this
for (int counter = 1; counter <= 10; counter++)
{
Console.WriteLine(counter*counter);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With