ank5
asked on
Convert last non zero digit of the number to 0
I need to write a program that would convert the last non zero digit of the number to 0, till all digits (except the first one are converted to 0). For instance, if the number supplied is "11550", then it should print
11500
11000
10000
similarly, if number supplied is 11841, then it should print
11840
11800
11000
10000
In my code, I am able to iterate over the digits
But I am not able to make out how to then transform the number? Do I need to replace the last digit and then append a 0 ?
Thank you
11500
11000
10000
similarly, if number supplied is 11841, then it should print
11840
11800
11000
10000
In my code, I am able to iterate over the digits
public class NumConversion {
public void convert(String strNum) {
int number = Integer.parseInt(strNum);
int tmp = 0;
while(number > 0) {
tmp = number % 10;
number /= 10;
System.out.println(tmp);
}
}
public static void main(String[] args) {
new NumConversion().convert("11550");
}
}
But I am not able to make out how to then transform the number? Do I need to replace the last digit and then append a 0 ?
Thank you
You'd probably be better off treating the number as a string
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.