• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Check For Harshad Number In Python

Check For Harshad Number In Python

Author: Aditya Raj
Last Updated: December 27, 2021

Numbers have many specialties. Based on the specialties, they are given unique names. One such special number is the Harshad number or Niven number. In this article, We will discuss a program to check if a given number is a Harshad number or not. 

What is a Harshad Number?

A number is said to be a Harshad number or Niven number if it is divisible by the sum of its digits. In other words, If we are given a number that is divisible by the sum of its own digits, the number will be called a Harshad Number. 

For example, let us consider the number 4320. The sum of its digits is 9 that can be obtained by adding the digits 4, 3, 2, and 0. We can see that 4320 is divisible by 9. Hence, 4320 is a Harshad number. On the other hand, the sum of digits in 4321 is 10. Here, 4321 is not completely divisible by 10. Hence, it is not a Harshad number.

Program To Check For A Harshad Number in Python

To check whether a number is a Harshad number or not, we will first calculate the sum of digits of the given number. After that, we will divide the number by the sum of digits to see if it is completely divisible or not. If the number is divisible by the sum of the digits, we will say that it is a Harshad Number. Otherwise not.

To write the complete program, we will first write a function to calculate the sum of digits of the given number. For this, we will keep dividing the number by 10 until the number becomes 0. Each time we divide the number by 10, we get the rightmost digit as the remainder. We can use this remainder to find the sum of digits by adding all the remainders till the number becomes 0.

The following function for calculating the sum of digits of a number in python accepts a number and returns the sum of digits of the number.

def calculate_sum_of_digits(N):
    sumOfDigits = 0
    while N > 0:
        digit = N % 10
        sumOfDigits = sumOfDigits + digit
        N = N // 10
    return sumOfDigits


input_number = 4320
output = calculate_sum_of_digits(input_number)
print("Sum of digits of {} is {}.".format(input_number, output))

Output:

Sum of digits of 4320 is 9.

After finding the sum of digits, we will divide the number by the sum of digits. If the reminder for the division is zero, we will say that a number is a Harshad number or Niven number. Otherwise, we will print that the number is not a Harshad Number.

def calculate_sum_of_digits(N):
    sumOfDigits = 0
    while N > 0:
        digit = N % 10
        sumOfDigits = sumOfDigits + digit
        N = N // 10
    return sumOfDigits


def check_for_harshad_number(N):
    sumOfDigits = calculate_sum_of_digits(N)
    if N % sumOfDigits == 0:
        return True
    else:
        return False


input_number = 4320
output = check_for_harshad_number(input_number)
print("{} is a Harshad Number:{}".format(input_number, output))
input_number = 4321
output = check_for_harshad_number(input_number)
print("{} is a Harshad Number:{}".format(input_number, output))

Output:

4320 is a Harshad Number:True
4321 is a Harshad Number:False

Conclusion

In this article, we have discussed what a Harshad number or Niven number is. We have also implemented a program in python to check if a given number is a Harshad number or not. To learn more about numbers in python, you can read this article on decimal numbers in python. You might also like this article on complex numbers in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us