Checksum routines often add the digits of a number with alternating signs instead of adding them all up.

Write a method that walks the digits of a number from right to left, adding the rightmost digit, subtracting the next one, adding the one after that, and so on. Return the resulting total.

Work with the absolute value, so a negative input gives the same answer as its positive twin. A null input returns 0.

Examples:

Input: 1234 Output: 2 Explanation: 4 - 3 + 2 - 1 equals 2 Input: 7 Output: 7 Explanation: A single digit is simply added Input: 0 Output: 0 Explanation: Zero has one digit and it adds to nothing Input: 90 Output: -9 Explanation: The rightmost 0 is added and the 9 is subtracted Input: null Output: 0 Explanation: A null input returns 0
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay December 2026

Contest runs December 1 - 31. Complete challenges to climb the leaderboard!

Prize 1: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In