0

1-3+5-7...n terms in java

Using scanner class

1st Jan 2020, 5:31 AM
Naira Mishra
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
1st Jan 2020, 7:12 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 2
I have given you the hints buddy. Please don't ask me to write it for you.
1st Jan 2020, 11:36 AM
Ipang
+ 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! 👍
1st Jan 2020, 6:05 AM
Ipang
+ 1
Okay dude i am trying according to your hint!!!!
1st Jan 2020, 11:39 AM
Naira Mishra