I'm trying to learn C# and I was interested in trying to write a simple do-while to calculate a square root of a simple number
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
double x = Convert.ToDouble(Console.ReadLine());
double root = 0;
do
{
root += 0.0001;
Console.WriteLine(root);
}
while ((root * root) % x != 0);
Console.WriteLine(Math.Sqrt(x));
Console.WriteLine(root);
}
}
}
If I use a round number for root += 0.0001; like root +=1; it works perfectly for even answers but once I start using 0.1 or smaller it breaks, and even ignores it's check in the while statement.
Can anybody explain why this happens? NOTE: I don't need a solution just the reason why this happens. And I know I can use Math.Sqrt(value);