1.I need someone to check if the program i wrote is correct.
"Write a program to calculate the sum (with accuracy of 0.001): 1 + 1/2 - 1/3 + 1/4 - 1/5 + ..." -> The code of the task is below.
2.I have one more question for example if i have "double a = 1/2" -> this return result 1 not 1.5, but
if i write "double a = (double)"1/2", then it will return result 1.5. Why i need to cast (double) before the expression ?
using System;
class Program
{
static void Main()
{
Boolean ready = false;
Boolean minus = false;
Boolean plus = true;
int i = 6;
double total = (double)(1 + 1 / 2 - 1 / 3 + 1 / 4 - 1 / 5);
while (ready == false)
{
if (plus == true)
{
total += (double)(1 / i);
plus = false;
minus = true;
i++;
}
else if (minus == true)
{
total -= (double)1 / i;
minus = false;
plus = true;
i++;
}
Console.WriteLine(total);
if (total < 0.001)
{
ready = true;
Console.WriteLine("accuracy of 0.001 number: {0}", total);
}
}
}
}