Count the Digits That Divide a Number

Count the Digits That Divide a Number

Given an integer num, return the number of digits in num that divide num.

An integer val divides nums if nums % val == 0.

LeetCode Problem - 2520

class Solution {
    public int countDigits(int num) {
        int count = 0; // Initialize a counter to keep track of divisible digits

        int copiedNum = num; // Create a copy of the input number to perform operations

        // Loop through each digit of the copied number
        while (copiedNum > 0) {
            int temp = copiedNum % 10; // Extract the last digit of the number

            // Check if the original number is divisible by the extracted digit
            if (num % temp == 0) count++;

            copiedNum /= 10; // Remove the last digit from the copied number
        }

        return count; // Return the total count of digits that divide the original number
    }
}