Rob Prime wrote:No need for the cast; char can be implicitly converted to int. Besides, the result of char + char is int anyway.
Just because you can omit something doesn't mean you should. I'm very much in favour of explicit casting because it clearly demonstrates your intentions and it removes many possible conversion mistakes like unintentional integer division to name just one.
Edward Chen wrote:No, I don't think this is correct solution. At least, we need to cast char to int , or do case-switch. Maybe has a better solution.
Here's another hint. Note that the formula I posted can be rewritten like,
int n = (((1*10 + 2)*10 + 3)*10 + 4)*10 + 5;
Can you see the pattern? If you consider the digits from left to right you're building up the number in steps. In each step you're multiplying the number from the previous step by ten and adding a digit, like