I have now written a simple square root calculator using the division method:
static void Main(string[] args)
{
double num, sqrt = 0;
int currentDecimal = 0, decimalAccuracyLevel, intAccuracyLevel = 5;
do
{
Console.Write("Enter your number: ");
} while (!double.TryParse(Console.ReadLine(), out num));
do
{
Console.Write("Enter the number of decimal points you wish to be accurate to: ");
} while (!int.TryParse(Console.ReadLine(), out decimalAccuracyLevel) && decimalAccuracyLevel >= 0);
while (0 < intAccuracyLevel)
{
double currentIncrement = Math.Pow(10, intAccuracyLevel);
while (Math.Pow(sqrt + currentIncrement, 2) <= num)
{
sqrt += currentIncrement;
}
intAccuracyLevel--;
}
while (currentDecimal <= decimalAccuracyLevel)
{
double currentIncrement = (double)1 / Math.Pow(10, currentDecimal);
while (Math.Pow(sqrt + currentIncrement, 2) <= num)
{
sqrt += currentIncrement;
}
currentDecimal++;
}
Console.WriteLine(sqrt);
}
Is there anything I should do to improve performance or accuracy?