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 firstname.lastname@example.org. 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.
- Maximum number formed from array with K number of adjacent swaps allowed
- Count number of triplets with product equal to given number with duplicates allowed
- Find minimum number to be divided to make a number a perfect square
- Count number of trailing zeros in Binary representation of a number using Bitset
- Given number of matches played, find number of teams in tournament
- Number of times the largest perfect square number can be subtracted from N
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Find the total number of composite factor for a given number
- Find the largest good number in the divisors of given number N
- Find count of digits in a number that divide the number
- Number of digits to be removed to make a number divisible by 3
- Count number of ways to divide a number in 4 parts
- Querying maximum number of divisors that a number in a given range has
- Find the smallest number whose digits multiply to a given number n
- Print a number strictly less than a given number such that all its digits are distinct.
Improved By : jit_t