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 n = 546 ;
int sum = 0;
while (n > 0)
{
sum =sum + n % 10;
n = n /10;
}
return sum;
}
}
Not sure what the error means.. Please help ?
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
YO MA
8 November 2019, 06:55
you can rather try this
int result = number/100 + ((number-99)/100) + ((number+99)/100); //5.46+4.47+6.45
return result;
0
poosa sudeshna
6 November 2019, 03:34
In your code, you didn't used the value of the variable that passed as a parameter in the method. Instead you have used hard coded value.
So remove the line10. And replace variable 'n' with 'number'. It should work.
0
Guadalupe Gagnon
5 November 2019, 18:31
The problem is that you hard coded "546" into your method. If you changed line 10 to pass in 657 to the method, it should output 18, but because it is hard coded it would still output 15. The method should accept any (three digit) number and return the sum of its digits.
0