I'm new to C# and C-like languages, and can't find the error in my code. Description of exercise:
Ticket numbers usually consist of an even number of digits. A ticket number is considered lucky if the sum of the first half of the digits is equal to the sum of the second half.
Given a ticket number n, determine if it's lucky or not.
Example
For n = 1230, the output should be isLucky(n) = true; For n = 239017, the output should be isLucky(n) = false. Input/Output
[execution time limit] 3 seconds (cs)
[input] integer n
A ticket number represented as a positive integer with an even number of digits.
Guaranteed constraints: 10 ≤ n < 106.
[output] boolean
true if n is a lucky ticket number, false otherwise.
My code:
bool isLucky(int n)
{
string ticket = n.ToString();
int firstSum = 0;
int secondSum = 0;
string[] ticketStrArr = ticket.Split("");
int lenHalf = ticketStrArr.Length / 2;
for(int i = 0; i < lenHalf; i++)
{
firstSum = firstSum + Int32.Parse(ticketStrArr[i]);
secondSum = secondSum + Int32.Parse(ticketStrArr[i + lenHalf]);
}
return (firstSum == secondSum);
}