The square root of a number written in a text file
You need to create a program that will read a number from a file and calculate the root of that number and write the result to a new text file. The problem is that the wrong answer is output, tell me what the error is
using (StreamReader sr = new StreamReader("file.txt"))
{
string numbers = sr.ReadLine();
int countOfElements;
int sqrtOfElement = 0;
String[] number = numbers.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
countOfElements = numbers.Length;
for (int count = 0; count < countOfElements; count++)
{
sqrtOfElement += Convert.ToInt32(Math.Sqrt(numbers[count]));
}
File.WriteAllText("result.txt", sqrtOfElement.ToString());
Console.WriteLine(sqrtOfElement);
Console.ReadKey();
}
If you write the number 9 in a text file, the result is 8 instead of 3
0