Skip to content

Instantly share code, notes, and snippets.

@HauptJ
Created June 3, 2024 01:04
Show Gist options
  • Save HauptJ/b664609d73b4dda3fe0c13de861b45a0 to your computer and use it in GitHub Desktop.
Save HauptJ/b664609d73b4dda3fe0c13de861b45a0 to your computer and use it in GitHub Desktop.
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;
}
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