Run a Loop of maximum digits of number

Hanish :

Eg. We have a number 355 then its digit counts are 3 . We have to write a program in java which run a loop from 100 to 999. If digits are 4 then run a loop from 1000 to 9999 If 5 then 10000 to 99999

Andreas :

If digits is 4, then your loop boundaries are 103 (1000) and 104 - 1 (9999).

In Java, you can use Math.pow(double a, double b) to calculate ab, making it easy to calculate the upper and lower boundaries:

int min = (int) Math.pow(10, digits - 1);
int max = min * 10 - 1;

Then you just write a for loop using those values.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=403548&siteId=1