Created
June 3, 2024 01:04
-
-
Save HauptJ/b664609d73b4dda3fe0c13de861b45a0 to your computer and use it in GitHub Desktop.
This file contains 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
You are given a two-digit integer n. Return the sum of its digits. | |
Example | |
For n = 29, the output should be | |
solution(n) = 11. | |
Input/Output | |
[execution time limit] 3 seconds (java) | |
[memory limit] 1 GB | |
[input] integer n | |
A positive two-digit integer. | |
Guaranteed constraints: | |
10 ≤ n ≤ 99. | |
[output] integer | |
The sum of the first and second digits of the input number. | |
[Java] Syntax Tips | |
// Prints help message to the console | |
// Returns a string | |
// | |
// Globals declared here will cause a compilation error, | |
// declare variables inside the function instead! | |
String helloWorld(String name) { | |
System.out.println("This prints to the console when you Run Tests"); | |
return "Hello, " + name; | |
} |
This file contains 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
int solution(int n) { | |
int sum = 0; | |
char [] numChars = String.valueOf(n).toCharArray(); | |
for(char c : numChars) { | |
sum += Character.getNumericValue(c); | |
} | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment