Link to home
Start Free TrialLog in
Avatar of ank5
ank5Flag for India

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

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");

	}

}

Open in new window


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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You'd probably be better off treating the number as a string
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial