Tuesday, December 28, 2021

Python for Beginners: Check For Harshad Number In Python

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.

The post Check For Harshad Number In Python appeared first on PythonForBeginners.com.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...