package com.codegym.task.task01.task0132;
/*
Sum of the digits of a three-digit number
*/
public class Solution {
public static void main(String[] args) {
System.out.println(sumDigitsInNumber(546));
}
public static int sumDigitsInNumber(int number) {
//write your code here
int sum=0;
int n=0;
while(number>1){
n=number%10;
sum=sum+n;
number=number/10;
}
return sum;
}
}
When i tried to run the program,15 was the answer.But when i try verifying ,the task fails to pass the testing and returns
The sumDigitsInNumber method must return the sum of all digits in the parameter number.
RECOMMENDATION FROM YOUR MENTOR
You need to add to the sum the remainder of dividing the parameter number by 10. Then set the variable number equal to the result (number/10). Repeat this operation three times.
What's wrong with my code
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Michael Martin
30 November 2018, 14:26
You don't need a while loop.
0
Sowmya
4 December 2018, 09:44
okay tq michael..
0