posted 5 years ago Show
I'm having trouble understanding/figuring out how i'm supposed to code the alternating sum of an inputed number /I suppose as in int??/ The code is supposed to take a number lets say 4321 and alternate + and - between the numbers. It can start either with - or + which really throws me off there are 2 shown inputs and expected outputs as an example: I'm really in a pinch here and i've got no idea what to do ;/ Saloon Keeper Posts: 5141 posted 5 years ago
There are three kinds of actuaries: those who can count, and those who can't. Petar Stoyanov Greenhorn Posts: 21 posted 5 years ago
As far as i can see he has the expected output and must figure out what signs to have between 1,2,3,4,5,6... to
get to that expected output the fastest Ranch Hand Posts: 109 posted 5 years ago
My two cents as a beginner in programming. Input 1 takes 1234. Do almost the same for the second output. posted 5 years ago
To alternate the sign, use these basic math facts: , you can easily flip it between -1 and +1 using the above. Marshal Posts: 76727 posted 5 years ago
You can “randomly” start with a different sign like this:-The () are redundant, but it is often worth using () around ?: because it has a low precedence. Another way to swap signs is like this:-The () are again redundant. How do you add alternate numbers in Java?println("Enter n:"); n = input. nextInt(); for (int i = 1; i <= n; i++) { if (i % 2 == 0){ sum-=input. nextInt(); } else { sum+=input. nextInt(); } } System.
How do you subtract two methods in Java?Subtraction in Java. int counter = 15;. counter = counter - 1;. System. out. println("Subtraction = " + counter);. What is subtract in Java?The java. math. BigDecimal. subtract(BigDecimal val) is used to calculate the Arithmetic difference of two BigDecimals. This method is used to find the arithmetic difference of large numbers without compromising with the precision of the result.
|