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 minus = false;
Boolean plus = true;
int i = 6;
double total = (double)(1 + 1 / 2 - 1 / 3 + 1 / 4 - 1 / 5);

{
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)
{

Console.WriteLine("accuracy of 0.001 number: {0}", total);
}
}
}
}
``````

Answer of 2nd question is 1/2 is took at integer and there are answer is converted to int. Thats why we cast it to double.

Do you mean to do 1 + 0.5 - 0.33 + 0.25?

or

1 + (half of total) - (third of total) + (quarter of total)?

It isnt very clear to me.

In reply to #2 [Majestics beat me to it while writing :(]

## All 8 Replies

Answer of 2nd question is 1/2 is took at integer and there are answer is converted to int. Thats why we cast it to double.

Do you mean to do 1 + 0.5 - 0.33 + 0.25?

or

1 + (half of total) - (third of total) + (quarter of total)?

It isnt very clear to me.

In reply to #2 [Majestics beat me to it while writing :(]

If we look at the structure of what your doing it is `double = integer / integer`

So the maths is done on integers and not doubles, therefore you casting the integers to doubles allows the correct result.

An alternative solution is `double A = 1.0/2.0;` this uses `double = double / double` so no cast is required.

Thank for the information about the second question.
The condition of the task is exactly as I wrote.I think the program code above is correct, but to be more sure i asked for check.

Well at present it gives an accuracy of `0.00601406511287088` so that still needs limiting to the three decimal places the task specifies.

You also have an issue with the finishing of the loop. When you set `ready` to true, you are on the number `-0.0162081571093513` which is then displayed as the final number instead of `0.00601406511287088 (should be 0.006)` therefore you need to store the prior number also when checking so you can go back to it, alternatively check the `sum of total + the next number` is not less than `0.001` before actually carrying out the sum.

now i understand that the point of the program is that the sum of all these numbers shouldn't be less than 0.001, and I must print the last posible sum of these numbers which is before 0.001 because there is (accuracy of 0.001) Am i right ?

I would personally interpret

with accuracy of 0.001

As the numbers outputted should be to three decimal places. Thats how I've seen it written many times in the past in terms of college work etc.

My point about the not below `0.001` was simply a correction to your code as it wasnt functioning how you wrote it to do so.

Line 12 starts total off with the wrong value and you should have `i` be a double to simplify the code.

Also, the problem wants your answer to 3 decimals of accuracy, not the answer when it falls below 0.001. So you need to check your current value vs the previous value and stop when it hasn't changed in the 3rd decimal place.

Lastly, the result of this is Pi/4, so if your answer isn't 0.785 your program isn't working :)

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 ?

If you don't need to cast double then you can use either both (i.e 1.0/2.0) or any one digit with decimal (i.e 1/2.0).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.