Advertisement
MagdalenaTakeva

Good numbers

Mar 24th, 2022
66
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3.  
  4.  
  5. public class GoodNumbers {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. //Read the input split by space, convert it to String Array
  9. String input = scanner.nextLine();
  10. String[] inputArr = input.split("\\s");
  11. int firstInt = Integer.parseInt(inputArr[0]);
  12. int finalInt = Integer.parseInt(inputArr[1]);
  13.  
  14. int totalGoodCount = 0;
  15.  
  16. for (int number = firstInt; number <= finalInt; number++) {
  17.  
  18. //Create a char array that will hold the digits of the generated number
  19. //Integer.toString(idx).toCharArray(); - converts number to string to char array
  20. char[] charArray = Integer.toString(number).toCharArray();
  21.  
  22. int count = 0;
  23.  
  24.  
  25. //Loop through the char array
  26. for (int k = 0; k < charArray.length; k++) {
  27.  
  28.  
  29. //get the int value of the character j
  30. int j = Character.getNumericValue(charArray[k]);
  31.  
  32.  
  33. // and check if the whole number can be divided by each of its digits
  34. if (j == 0 || (number % j) == 0) {
  35.  
  36. //if yes, the number is divisible by j and count is incremented by 1
  37. count++;
  38.  
  39. }
  40. }
  41.  
  42. //if count of digits that are divisible equals char array length
  43. //this means that the number is good
  44. if (count == charArray.length) {
  45. System.out.printf("%d good\n", number);
  46. totalGoodCount++;
  47. } else {
  48. System.out.printf("%d not good\n", number);
  49. }
  50.  
  51.  
  52. }
  53. System.out.println(totalGoodCount);
  54. }
  55.  
  56.  
  57. }
Advertisement
RAW Paste Data Copied
Advertisement