Skip to content

Instantly share code, notes, and snippets.

@ShivangiM
Last active September 11, 2023 08:11
Show Gist options
  • Save ShivangiM/eda519de5f30d6986820de5a26bf7bf6 to your computer and use it in GitHub Desktop.
Save ShivangiM/eda519de5f30d6986820de5a26bf7bf6 to your computer and use it in GitHub Desktop.
CracklePop Number Printer. Of number 1 to 10, If the number is divisible by 3, print Crackle instead of the number. If it's divisible by 5, print Pop. If it's divisible by both 3 and 5, print CracklePop.
public class NumberPrinter {
public static void main(String[] args) {
NumberPrinter printer = new NumberPrinter();
printer.printNumbers(1, 100);
}
public void printNumbers(int start, int end) {
for (int number = start; number <= end; number++) {
String result = generateOutput(number);
System.out.println(result);
}
}
private String generateOutput(int number) {
if (number % 3 == 0 && number % 5 == 0) {
return "CracklePop";
} else if (number % 3 == 0) {
return "Crackle";
} else if (number % 5 == 0) {
return "Pop";
} else {
return String.valueOf(number);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment