Skip to content

Instantly share code, notes, and snippets.

@tomasbaran
Last active April 3, 2024 22:17
Show Gist options
  • Save tomasbaran/f4ad73e7c4ff2d43187d4c7e7a4e1f97 to your computer and use it in GitHub Desktop.
Save tomasbaran/f4ad73e7c4ff2d43187d4c7e7a4e1f97 to your computer and use it in GitHub Desktop.
isPalindrome
bool isPalindrome(int number) {
// Core logic explained on an example 1234
// 1234 == 4321
// old new
// Default values
// 1234 0
// Loop starts:
// 123 4 (0*10 +4)
// 12 43 (4*10+3)
// 1 432 (43*10 +2)
// 0 4321 (432*10 + 1)
int oldNumber = number;
int newNumber = 0;
// Convert the number to its reverse until the old number is 0
while (oldNumber != 0) {
newNumber = newNumber * 10 + oldNumber % 10;
oldNumber ~/= 10;
}
return number == newNumber;
}
void main() => print(isPalindrome(1221));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment