Petar Stoyanov

Greenhorn

Posts: 21

posted 1 year ago

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 ;/

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 ;/

Piet Souris

Master Rancher

Posts: 2044

75

posted 1 year ago

Is your question related in any way to this topic:

https://coderanch.com/t/672251/java/Find-number-minimum-operations-number

https://coderanch.com/t/672251/java/Find-number-minimum-operations-number

Petar Stoyanov

Greenhorn

Posts: 21

posted 1 year 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

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

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

posted 1 year ago

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.

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 1 year ago

To alternate the sign, use these basic math facts:

If you have a variable,

If you have a variable,

`sign`, you can easily flip it between -1 and +1 using the above.*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

It is sorta covered in the JavaRanch Style Guide. |