N
N
Nar Nar2020-07-07 12:43:23
C++ / C#
Nar Nar, 2020-07-07 12:43:23

Need to find the sum of the last and from the end of the third digit of the number 12345689?

I turned it into a character array, but in the end I got a number code instead of the sum, what's the problem?

string bar = "123456789";

            char[] barArr = sw.ToCharArray();
            
            int c;
            for (int i = 0; i < barArr.Length; i++)
            {
                int sum = barArr[barArr.Length - 3] + barArr[barArr.Length - 1];

            }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#, 2020-07-07
@narsss

var sum = (x % 1000)/100 + x % 10;
more details here https://dotnetfiddle.net/0BhxjK
ps

var sum = (x[x.Length-1] - '0') + (x[x.Length-3] - '0');

more details here https://dotnetfiddle.net/BSi9QQ

Y
yuopi, 2020-07-07
@yuopi

Because the operation is concatenating 2 strings, not adding numbers
int sum = int.Parse(barArr[barArr.Length - 3]) + int.Parce(barArr[barArr.Length - 1]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question