S.M_Emamian S.M_Emamian - 3 months ago 16
Java Question

All the possible 4 digit combinations of 0-9

My question is simple, I would like to print all the possible 4 digit combinations of 0-9:

Like:

0001
0002
0003
.
.
.
0009
0010
0011
.
.
.
9991
9992
9993
9994
9995
9996
9997
9998
9999


How can I do?

Answer

Put one for loopa and iterate from 0 to 9999

Set conditions inside for loop also, if value is less than 10 than add three 0, if value is less than 100 than add two 0, if value is less than 1000 than add one 0

As you want all 4 digit combinations of 0 to 9, 0000 will also be accounted.

for(int i=0; i<=9999; i++){
    if(i<10){
        System.out.println("000"+i);
    }
    else if(i<100){
        System.out.println("00"+i);
    }
    else if(i<1000){
        System.out.println("0"+i);
    }
    else{
        System.out.println(i);
    }
}