Please tell me the logic for the code.

For eg. 2341 then we subtract the adjacent terms with each 2-3=1
2-4=1
4-1=3
then after subtracting we get :113
then we minus 1-1=0
1-3=2
that is the single term which is 2 this must be the answer.
Please tell the Logic For code

Keep generating the last and the second last digits of the number and subtract them. Use the resultant to form a new number. Once the new number has been formed, repeat the above steps with the new number until we get a single digit new number. That must be the answer.
For eg. 2341, on generating the last and the second last digits of the number and subtracting them, we get,
4-1=3
3-4=1
2-3=1
The new number formed is 113. Repeating the above steps with 113 we get,
1-3=2
1-1=0
The new number formed is 2 which is a single digit number and thus the answer.