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.
- 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
- Maximum number formed from array with K number of adjacent swaps allowed
- 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
- Find the number of integers x in range (1,N) for which x and x+1 have same number of divisors
- Program to Convert Octal Number to Binary Number
- Super Ugly Number (Number whose prime factors are in given set)
- Count number of digits after decimal on dividing a number
- Number of digits to be removed to make a number divisible by 3
- Find the number of ways to divide number into four parts such that a = c and b = d
- Find the smallest number whose digits multiply to a given number n
- Find the maximum number of composite summands of a number
- Count number of ways to divide a number in 4 parts
Improved By : jit_t