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.
- 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
- 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 possible permutations when absolute difference between number of elements to the right and left are given
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Number of ways to split a binary number such that every part is divisible by 2
- Given number of matches played, find number of teams in tournament
- Number of times the largest perfect square number can be subtracted from N
- Count number of digits after decimal on dividing a number
- Find the number of ways to divide number into four parts such that a = c and b = d
- Find count of digits in a number that divide the number
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Find the maximum number of composite summands of a number
- Querying maximum number of divisors that a number in a given range has
Improved By : jit_t