Python Program to Check If a Number is a Harshad Number Last Updated : 15 Apr, 2024 Comments Improve Suggest changes Like Article Like Report Harshad Numbers can be divided by the sum of its digits. They are also called Niven Numbers. For instance, 18 is a Harshad Number as it can be divided by 9, the sum of its digits (8+1=9). In this article, we will discuss various approaches to determine whether the given number is a Harshad Number in Python. Example: Input: 18 Output: 18 is a Harshad Number Input: 15 Output: 15 is not a Harshad NumberPython Program to Check If a Number is a Harshad NumberBelow are some of the approaches by which we can check if a number is a Harshad Number using Python: Using LoopUsing StringUsing LoopIn this example, the checkHarshad function examines whether a number is a Harshad number by verifying if the sum of its digits is divisible by the number itself. If it is, it returns "Yes"; otherwise, it returns "No". Python3 def checkHarshad(n): sum = 0 temp = n while temp > 0: sum = sum + temp % 10 temp = temp // 10 # Return true if sum of digits is multiple of n return n % sum == 0 if(checkHarshad(12)): print("Yes") else: print("No") if (checkHarshad(15)): print("Yes") else: print("No") OutputYes No Using StringIn this example, we are using str() function to check if a number is a Harshad Number or not. Python3 def checkHarshad(n): # Converting integer to string st = str(n) sum = 0 length = len(st) for i in st: sum = sum + int(i) # Comparing number and sum if (n % sum == 0): return "Yes" else: return "No" number = 18 print(checkHarshad(number)) number = 15 print(checkHarshad(number)) OutputYes No Comment More infoAdvertise with us Next Article Python Program to Check If a Number is a Harshad Number B bytebarde55 Follow Improve Article Tags : Python Python Programs python-basics Python-DSA Practice Tags : python Similar Reads Python program to check if the given number is Happy Number A number is called happy if it leads to 1 after a sequence of steps wherein each step number is replaced by the sum of squares of its digit that is if we start with a Happy Number and keep replacing it with digits square sum, we reach 1. In this article, we will check if the given number is a Happy 4 min read Python Program to Check if a Number is Odd or Even Even Numbers are exactly divisible by 2 and Odd Numbers are not exactly divisible by 2. We can use modulo operator (%) to check if the number is even or odd. For even numbers, the remainder when divided by 2 is 0, and for odd numbers, the remainder is 1.In this article, we will learn how to check if 2 min read Check if a Number is a Whole Number in Python Floating-point numbers in Python can sometimes pose challenges when you need to determine whether they represent whole numbers. Due to the inherent precision limitations of floating-point representation, comparing them directly for equality with integers may lead to unexpected results. In this artic 3 min read Python Program to Check Number is a Power of Two we will discuss how to write a Python program to determine whether a given number is a power of two. A number is said to be a power of two if it can be expressed in the form of 2n, where n is a non-negative integer.Examples: Pythondef is_power_of_two(n): if n <= 0: return False return (n & (n 4 min read Python program to check if number is palindrome (one-liner) In this article, we are given a number and we have to check whether the number is palindrome or not in one-liner code. The output will be True if it's a Palindrome number otherwise it would be False. Let's discuss how to find whether a number is palindrome or not in this article. Input1: test_number 3 min read Like