Harshad Number:
A Harshad number is one whose original number is divisible by the sum of its digits.
like 5 , 18 , 156 etc.
Example 1:
Input:
number=18
Output:
18 is harshad number
Explanation:
Here sum_of_digits=9 i.e (1+8 ) and 18 is divisible by 9
Example 2:
Input:
number=19
Output:
19 is not harshad number
Explanation:
Here sum_of_digits=10 i.e (1+ 9 ) and 19 is not divisible by 10
- Python Program to Print all Harshad Numbers within Given Range
- Python Program to Check Armstrong Number
- Python Program to Find the LCM of Two Numbers
Harshad Number in Python
Below are the ways to check harshad number in python
- Using while loop
- By converting the number to string and Traversing the string to extract the digits
- Using list and map
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
Method #1: Using while loop
Algorithm:
- Scan the input number
- Make a copy of the number so you can verify the outcome later.
- Make a result variable ( set to 0 ).
- Create a while loop to go digit by digit through the number.
- Every iteration, increase the result by a digit.
- Divide the result by the number’s duplicate.
- If a number divides perfectly, it is a Harshad Number; otherwise, it is not.
Below is the implementation:
# given number num = 18 # intiialize sum of digits to 0 sum_of_digits = 0 # copy the number in another variable(duplicate) dup_number = num # Traverse the digits of number using for loop while dup_number > 0: sum_of_digits = sum_of_digits + dup_number % 10 dup_number = dup_number // 10 # It is harshad number if sum of digits is equal to given number if(num % sum_of_digits == 0): print(num, "is harshad number") else: print(num, "is not harshad number")
Output:
18 is harshad number
Method #2: By converting the number to string and Traversing the string to extract the digits
Algorithm:
- Using a new variable, we must convert the given number to a string.
- Iterate through the string, convert each character to an integer, and add the result to the sum.
- If a number divides perfectly, it is a Harshad Number; otherwise, it is not.
Below is the implementation:
# given number num = 18 # Converting the given number to string numString = str(num) # intiialize sum of digits to 0 sum_of_digits = 0 # Traverse through the string for char in numString: # Converting the character of string to integer and adding to sum_of_digits sum_of_digits = sum_of_digits + int(char) # It is harshad number if sum of digits is equal to given number if(num % sum_of_digits == 0): print(num, "is harshad number") else: print(num, "is not harshad number")
Output:
18 is harshad number
Method #3: Using list and map
Algorithm:
- Convert the digits of given number to list using map function.
- Calculate the sum of digits using sum() function.
- If a number divides perfectly, it is a Harshad Number; otherwise, it is not.
Below is the implementation:
# given number num = 18 # Converting the given number to string numString = str(num) # Convert the digits of given number to list using map function numlist = list(map(int, numString)) # calculate sum of list sum_of_digits = sum(numlist) # It is harshad number if sum of digits is equal to given number if(num % sum_of_digits == 0): print(num, "is harshad number") else: print(num, "is not harshad number")
Output:
18 is harshad number
Related Programs:
- python get number of elements in a list list of lists or nested list
- python count number of true elements in numpy array in python
- disarium number in python
- write a program to reverse a number in python
- python program to count the number of digits present in a number
- python program to find the missing number in an array list
- python program to count set bits in a number