Hi,

I am trying to write a loop that lists all the square results until the number given in.

So for example if i would give in 100 it would list the squares in a list wich would be 1, 4, 9, 16 etc...

The problem is i if i increase the count i am getting wrong results wich go from 1, 4, 25, .....I suppose i need to work with a variable or?

A bit of a stupid question, but i cannot seem to get it to work. (ive tried both a while and a for version).

The value of the count keeps being "reminded" so it will keep counting up incorrect values. So how do i obtain 1*1, 2*2, 3*3, 4*4 , 5*5, ...?
So the proper values will apear as 1, 4, 9 , 16 on the screen.

Regards.

Console.Write("Give in a number...");
      ulong Number = ulong.Parse(Console.ReadLine());
      ulong Count = 1;
      while (Count <= Number)
      {
        Count = Count * Count;
        Console.WriteLine("{0}", Count);
        Count = Count + 1;
      }

Edited 7 Years Ago by Nerathas: n/a

Try this:

while (Count * Count <= Number)
{
      Console.WriteLine("{0}", Count * Count);
      Count = Count + 1;
}
while (Count <= Number)      {        Count = Count * Count;        Console.WriteLine("{0}", Count);        Count = Count + 1;      }Console.Write("Give in a number...");
      ulong Number = ulong.Parse(Console.ReadLine());
      ulong Count = 1;
      while (Count <= Number)
      {
        Count = Count * Count; // <-- you're problem is here
        Console.WriteLine("{0}", Count);
        Count = Count + 1;
      }

You are assigning a new value to count. Any time you find a variable behaving unexpectedly it is a good idea to follow your code through line by line to see what value it holds at each stage of processing:

First Iteration Start	1
Count = Count * Count	1
Count = Count + 1	         2
	
Second iteration Start:	2
Count = Count * Count	4
Count = Count + 1	          5
	
Third Iteration Start:	5
Count = Count * Count	25
Count = Count + 1	          26

Edited 7 Years Ago by Geekitygeek: n/a

This question has already been answered. Start a new discussion instead.