java - Can't understand logic of code to find the sum of the evenly positioned digits -


So the question is basically something like:

to accept a number Write a program for the user and calculate the digits deployed evenly from the left side. For example if the input is 94852 then the output is 9 (4 + 5).

Now, I know that this question can be done by changing the number in the string and using the array, but in fact there is a better way to do this.

The following program shows this method:

  public class questions {public static zero main (int one) {int evenSum = 0, odd = 0; Int b = a; // b is used as a value, eventually gets 0. Whereas (a & gt; 0) {int sum = evenSum + a% 10; Bhima = Asymmetrical; // change the amount (?) Strange thing = yoga; // Interestingly, weird odd positioed points reserves the amount of a = a / 10; } System.out.println ("entered number is" + B); System.out.println ("Even the amount is" + Evening in the evening); }}   

When I was scouting for alternative solutions, I happened to come to this reply, and it was stunned to me. I could not understand the logic.

So will someone please explain this method what does this method and why it works?

The code repeatedly changes a number for the correct (integer division from 10). It takes the most points by modulo 10 (the remainder of the integer division).

It is alternately in the loop in the next phase in the second loop in the sum of one step in a sum.

Then there are a couple of quotes: "weird" and "even" points.

However, as you say "weird" and "even" for posts from left , and the loop starts with right You can not say on the first step, in the first digits, this digit is a weird or even position (counting from left).

So you need to include both odd and even both posts.

And by swapping the odd and even negative at each stage, you ensure that the last digit is always added to the strange sum, even in fact "even "Yoga.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -