Skip to content

Instantly share code, notes, and snippets.

@youssef3wi
Created November 22, 2023 15:02
Show Gist options
  • Select an option

  • Save youssef3wi/d9cfb2679ef89beeaacb917dfa95162a to your computer and use it in GitHub Desktop.

Select an option

Save youssef3wi/d9cfb2679ef89beeaacb917dfa95162a to your computer and use it in GitHub Desktop.
Sum digits of a string.
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