JAVA/Practice Q
Q.1~10000사이에 8이 몇번 나오는가?
민지짱민지최고
2023. 9. 20. 09:34
// while
int x = 1;
int count = 0;
while(x <= 10000) {
int y = x;
while(y > 0) {
if(y % 10 == 8) {
count++;
}
y /= 10;
}
x++;
}
System.out.println(count);
// for
int count = 0;
for(int x = 1 ; x <= 10000 ; x++) {
for(int y = x ; y > 0 ; y/= 10) {
if(y % 10 == 8) {
count++;
}
}
}
System.out.println(count);