Sum of alternate numbers in Java

posted 5 years ago

  • Sum of alternate numbers in Java
  • Sum of alternate numbers in Java

    Number of slices to send:

    Optional 'thank-you' note:

  • Sum of alternate numbers in Java
  • Sum of alternate numbers in Java

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:
input1= 101 => output1= 2 , we take  +1-0+1=2
input2= 42 => output2= -2 , which should mean we take the first number as "-" => -4+2=-2

I'm really in a pinch here and i've got no idea what to do ;/

Saloon Keeper

Sum of alternate numbers in Java

Posts: 5141

posted 5 years ago

  • Sum of alternate numbers in Java
  • Sum of alternate numbers in Java

    Number of slices to send:

    Optional 'thank-you' note:

  • Sum of alternate numbers in Java
  • Sum of alternate numbers in Java

There are three kinds of actuaries: those who can count, and those who can't.

Petar Stoyanov

Greenhorn

Posts: 21

posted 5 years ago

  • Sum of alternate numbers in Java
  • Sum of alternate numbers in Java

    Number of slices to send:

    Optional 'thank-you' note:

  • Sum of alternate numbers in Java
  • Sum of alternate numbers in Java

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
i just have random input numbers 231,412,51... and can get 2 outputs basicly - 1 starting from + then - and alternating or starting from - and then + and so on.
I have to brake down a number ex. 231 into 2,3,1 and then alternate +,- between them

Ranch Hand

Posts: 109

Sum of alternate numbers in Java

posted 5 years ago

  • Sum of alternate numbers in Java
  • Sum of alternate numbers in Java

    Number of slices to send:

    Optional 'thank-you' note:

  • Sum of alternate numbers in Java
  • Sum of alternate numbers in Java

My two cents as a beginner in programming.

Input 1 takes 1234.
Do while number is not 0
{1234%10=4.
Store 4 in the array with length arraylength=(int)(Math.log10(n)+1).
1234=1234/10}
for(i=0;i<arraylength;i++)
//create a sum where you alternate the sign using a variable which assigns itself - for iteration 1 and back to + to iteration 2.

Do almost the same for the second output.

posted 5 years ago

  • Sum of alternate numbers in Java
  • Sum of alternate numbers in Java

    Number of slices to send:

    Optional 'thank-you' note:

  • Sum of alternate numbers in Java
  • Sum of alternate numbers in Java

To alternate the sign, use these basic math facts:


If you have a variable,

sign

, you can easily flip it between -1 and +1 using the above.

Sum of alternate numbers in Java

Marshal

Sum of alternate numbers in Java

Posts: 76727

posted 5 years ago

  • Sum of alternate numbers in Java
  • Sum of alternate numbers in Java

    Number of slices to send:

    Optional 'thank-you' note:

  • Sum of alternate numbers in Java
  • Sum of alternate numbers in Java

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.

Sum of alternate numbers in Java

Sum of alternate numbers in Java

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.