Created
November 22, 2023 15:02
-
-
Save youssef3wi/d9cfb2679ef89beeaacb917dfa95162a to your computer and use it in GitHub Desktop.
Sum digits of a string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import static java.lang.Integer.parseInt; | |
| public class CodingGame { | |
| public static void main(String[] args) { | |
| String number = "34267"; | |
| int result = 0; | |
| int oddSum = 0; | |
| for (int idx = 0; idx < number.length(); idx++) { | |
| int digit = parseInt(number.charAt(idx) + ""); | |
| if (idx % 2 == 0) { | |
| oddSum += digit; | |
| } else { | |
| result += digit; | |
| } | |
| } | |
| result += (oddSum * 3); | |
| if (result % 10 != 0) { | |
| result -= 10; | |
| } | |
| System.out.printf("RESULT = %d%n", result); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment