Generation

generate functionTue, 18 Apr 2023

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.

bool isLucky(int n) { string s = n.ToString(); int[] arr = new int[s.Length]; for(int i = 0; i < s.Length; i++) { arr[i] = int.Parse(s[i].ToString()); } int midpoint = arr.Length/2; int sum1 = 0; int sum2 = 0; if(arr.Length % 2 != 0) { return false; } for(int j = 0; j < midpoint; j++) { sum1 += arr[j]; } for(int k = midpoint; k < arr.Length; k++) { sum2 += arr[k]; } if(sum1 == sum2) { return true; } return false; }

Want to kickstart your project?Use the new AI Studio to create your code