Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Sum1With1DividedByNWithPrecision
- {
- static void Main()
- {
- //Write a program to calculate the sum (with accuracy of 0.001): 1 + 1/2 - 1/3 + 1/4 - 1/5 + ...
- Console.Write("Please enter the end number of the interval:");
- int numberOfelements = int.Parse(Console.ReadLine());
- decimal sum = 1m;
- int i = 0;
- for (i = 2; i <= numberOfelements; i += 2)
- {
- sum += (1 / (decimal)i) - (1 / (decimal)(i + 1));
- }
- Console.WriteLine("The sum is: {0:.000}", sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement