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) {
int sum = 0;
for (int i = number; i > 0; i /= 10){
int remainder = number % 10;
sum += remainder;
} return sum;
}
}
Can anyone please tell me what i'm doing wrong here? My output is 18 instead of 15. I want to complete this using a for loop and not a while loop.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) {
int sum = 0;
while(number > 0) {
sum = sum + number % 10;
number = number /10;
} return sum;
}
}