0
1-3+5-7...n terms in java
Using scanner class
4 Réponses
+ 6
Parash Pandey
You can make its another solution or verify your solution(after making use of hints suggested by Ipang)
Another way can be pairing every 2 consecutive terms like this:
if n is even:
(1-3)+(5-7)+(9-11)+...+((2n-3)-(2n-1))
= (-2)+(-2)...+(-2) [n/2 times]
= -2(n/2) = -n
and if n is odd, then it will look like:
(1-3)+(5-7)+(9-11)+...+((2n-1) - no term here)
= (-2)+(-2)+...+(-2)[(n-1)/2 times] + (2n-1)
= (1-n)+(2n-1) = n
+ 2
I have given you the hints buddy. Please don't ask me to write it for you.
+ 1
Please tag Java, your question's preferred language.
Hint:
* Have a boolean variable used to decide when to add or to subtract.
* Make a loop intialized with 1 with 2 number increment steps
* In each loop iteration you invert the boolean variable value after performing the operation.
Share what you tried to show a good will for trying. Avoid asking for complete solution.
Good luck! 👍
+ 1
Okay dude i am trying according to your hint!!!!