display multiples of 5 to 5000 using the while loop,for loop and do while loop
class App {
public static void main(String[] args) {
// for loop
System.out.println("for loop:");
for (int i = 0; i <= 5000; i++) {
if (i % 5 == 0) {
System.out.println(i);
}
}
// while loop
System.out.println("\n\nwhile loop:");
int counter = 0;
while (counter <= 5000) {
if (counter % 5 == 0) {
System.out.println(counter);
}
counter++;
}
// do while loop
System.out.println("\n\ndo while loop:");
counter = 0;
do {
if (counter % 5 == 0) {
System.out.println(counter);
}
counter++;
} while (counter <= 5000);
}
}
Comments
Leave a comment