An integer number in base 10 which is divisible by sum of it digits is said to be a Harshad Number. An n-harshad number is an integer number divisible by sum of its digit in base n.
Below are first few Harshad Numbers represented in base 10:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20………
Given a number in base 10, our task is to check if it is a Harshad Number or not.
Input: 3 Output: 3 is a Harshad Number Input: 18 Output: 18 is a Harshad Number Input: 15 Output: 15 is not a Harshad Number
1. Extract all the digits from the number using % operator and calculate the sum.
2. Check if the number is divisible by sum.
Below is implementation of above idea:
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of trailing zeros in Binary representation of a number using Bitset
- Find minimum number to be divided to make a number a perfect square
- Number of times the largest perfect square number can be subtracted from N
- Given number of matches played, find number of teams in tournament
- Number of digits to be removed to make a number divisible by 3
- Count number of ways to divide a number in 4 parts
- Print a number strictly less than a given number such that all its digits are distinct.
- Queries on sum of odd number digit sums of all the factors of a number
- Super Ugly Number (Number whose prime factors are in given set)
- Find the largest good number in the divisors of given number N
- Minimum number of given powers of 2 required to represent a number
- Querying maximum number of divisors that a number in a given range has
- Check if a number is divisible by all prime divisors of another number
- Find count of digits in a number that divide the number
Improved By : jit_t