Reputation: 43
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();
Upvotes: 4
Views: 34356
Reputation: 1
Since you start with 1, that counter * counter can not be 0. So, having that in mind, here's the whole code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
Console.WriteLine(i * i);
}
}
}
I am sure that this was helpful.
Upvotes: 0
Reputation: 38200
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
Upvotes: 5
Reputation: 5192
if ((counter * counter) == 0) This will not satisfy for any value..Try if ((counter * counter) != 0) ..Try this..
Upvotes: 1
Reputation: 41665
For an integer counter
having any value other than 0
, counter * counter
will never evaluate to 0
.
Upvotes: 2
Reputation: 30698
Try this
for (int counter = 1; counter <= 10; counter++)
{
Console.WriteLine(counter*counter);
}
Upvotes: 3