Write a program that asks the user to input a positive number (declare an integer variable n). Your program should calculate the sum of the following alternating series. Note that the sign alternates between + and -. Also make sure that the divisions performed within the loop are not integer type divisions. Otherwise, sum of series will always be 1 for any n. For example, 1/2 is 0 in C++ since it is an integer division. So, you need to do static_cast<double> in the division operation. Display the sum in fixed, show point format with exactly 6 significant digits after the decimal point.
1 −1/2 + 1/3 − 1/4 + 1/5 − 1/6 + ⋯1/𝑛
I have no clue what algorithm I'm using in order to add and subtract following each other.
Can someone tell me what I can change in order to properly run this and add/subtract the sequence of divided numbers leading up to n?
What is the pattern of the series?
Even denominators are subtracted, while odd denominators are added.
So the test is simple. Determine if n is even or odd.
Hint: Use the modulo operator (%).
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You've got the right idea checking count for even or odd.
However, you're not computing each term. Each term is 1/count. Pay attention to what was stated in the problem and be careful to NOT do integer division. Incrementing and decrementing num makes no sense.
Line 23: You can't put a condition on an else clause. In fact, you don't need the condition whatsoever. If it's not even, then guess what, it must be odd.
So I am supposed to divide by the count, but I just couldn't figure out exactly what I was doing wrong.. I really do appreciate the help. So i had the modulus correct, it was just the math after that. -= and += divided by count.