Given an integer num
, return the number of digits in num
that divide num
.
An integer val
divides nums
if nums % val == 0
.
Example 1:
Input: num = 7 Output: 1 Explanation: 7 divides itself, hence the answer is 1.
Example 2:
Input: num = 121 Output: 2 Explanation: 121 is divisible by 1, but not 2. Since 1 occurs twice as a digit, we return 2.
Example 3:
Input: num = 1248 Output: 4 Explanation: 1248 is divisible by all of its digits, hence the answer is 4.
Constraints:
1 <= num <= 109
num
does not contain 0
as one of its digits.program main
! Solves the problem: Given an integer `num`, return the number of digits in `num` that divide `num`.
implicit none
integer :: num, i, j, count
character(len=10) :: str
! Examples
num = 7
call solve(num)
write (*,*) "Example 1:"
write (*,*) "Input:", num
write (*,*) "Output:", count
write (*,*) ""
num = 121
call solve(num)
write (*,*) "Example 2:"
write (*,*) "Input:", num
write (*,*) "Output:", count
write (*,*) ""
num = 1248
call solve(num)
write (*,*) "Example 3:"
write (*,*) "Input:", num
write (*,*) "Output:", count
write (*,*) ""
contains
subroutine solve(num)
implicit none
integer, intent(in) :: num
integer :: i, j
character(len=10) :: str
! Convert `num` to string
write (str, '(I10)') num
! Count the number of digits that divide `num`
count = 0
do i = len(str), 1, -1
do j = 1, i-1
if (str(j:i) == str(i:)) then
count = count + 1
exit
end if
end do
end do
end subroutine solve
end program main
Example 1: Input: 7 Output: 0 Example 2: Input: 121 Output: 0 Example 3: Input: 1248 Output: 0
! Declare the input variable
integer :: num
! Declare the output variable
integer :: result
! Read the input
read(*,*) num
! Initialize the result
result = 0
! Loop through the digits of num
do i = 1, len_trim(num)
! Extract the current digit
digit = num(i:i)
! Check if the digit divides num
if (num mod digit == 0) then
! Increment the result
result = result + 1
end if
end do
! Print the result
write(*,*) result
end
temp.f95:16:5: 16 | digit = num(i:i) | 1 Error: Unclassifiable statement at (1) temp.f95:19:13: 19 | if (num mod digit == 0) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:22:7: 22 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:14:19: 14 | do i = 1, len_trim(num) | 1 Error: βstringβ argument of βlen_trimβ intrinsic at (1) must be CHARACTER
def digits_dividing_num(num):
count = 0
temp = num
while temp > 0:
digit = temp % 10
if num % digit == 0:
count += 1
temp //= 10
return count
The algorithm for counting the number of digits in `num` that divide `num` is as follows:
count
to store the count of such digits, and set it to 0.temp
equal to the given num
.temp
is greater than 0, follow these steps:
a. Find the last digit of temp
by calculating the remainder of the division temp % 10
.
b. Check if num % digit
is equal to 0; if so, increment the count
by 1.
c. Remove the last digit of temp
by integer dividing it by 10 (i.e., temp /= 10
in C++ and Java, temp //= 10
in Python, or temp = Math.floor(temp / 10)
in JavaScript).count
.Each language implements these steps in a fairly similar way, with only minor syntax differences between them.
int digitsDividingNum(int num) {
int count = 0, temp = num;
while (temp > 0) {
int digit = temp % 10;
if (num % digit == 0) {
count++;
}
temp /= 10;
}
return count;
}
The algorithm for counting the number of digits in `num` that divide `num` is as follows:
count
to store the count of such digits, and set it to 0.temp
equal to the given num
.temp
is greater than 0, follow these steps:
a. Find the last digit of temp
by calculating the remainder of the division temp % 10
.
b. Check if num % digit
is equal to 0; if so, increment the count
by 1.
c. Remove the last digit of temp
by integer dividing it by 10 (i.e., temp /= 10
in C++ and Java, temp //= 10
in Python, or temp = Math.floor(temp / 10)
in JavaScript).count
.Each language implements these steps in a fairly similar way, with only minor syntax differences between them.