HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Square Root Calculator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
calculatorrootsquare

Problem

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

Is there anything I should do to improve performance or accuracy?

Solution

Declare your variables a near as possible to their usage.

Initializing multiple variables on a single line removes readability and can lead in some languages to unexpected results.

If you divide two numbers and one of the numbers is a double the result is not a integer division result, hence you don't need the cast to double.

Because you are reinventing the wheel, it would be appropriate to do the Pow() by your code, because otherwise you could simply use Math.Pow(num, 0.5).

You should name your variables better. The int currentDecimal variable is storing the current calculated decimal points, but it reads that it holds the current Decimal number. You should rename it to currentDecimalPoint or something more meaningful.

This is a small application, but you should nevertheless extract the reading of the user input and the calculation of the result to separate methods.

Context

StackExchange Code Review Q#78923, answer score: 4

Revisions (0)

No revisions yet.