- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

When it is required to determine if a given number is a Harshad number or not, a simple loop and ‘%’ operator, ‘+’ operator and ‘//’ operators can be used.

A Harshad number is also known as a Niven number. It is a number whoses base is an integer that can be divided by the sum of its digits when it is written as that base value.

Below is a demonstration for the same −

my_num = 134 remaining = sum_val = 0 print("A copy of the number to be checked is being made...") my_num_copy = my_num; while(my_num > 0): remaining = my_num%10; sum_val = sum_val + remaining; my_num = my_num//10; if(my_num_copy % sum_val == 0): print(str(my_num_copy) + " is a Harshad number"); else: print(str(my_num_copy) + " isn't a Harshad number");

A copy of the number to be checked is being made... 134 isn't a Harshad number

- A number is defined.
- Another sum variable is assigned the value 0.
- The number is checked to be greater than 0, and modulus operation is performed on it.
- This result is added to the ‘sum’ and the number is again floor divided by 10.
- If the remainder when the number and sum are divided is 0, it is considered as a Harshad number.
- Otherwise, it is not considered as a Harshad number.

- Related Questions & Answers
- Python Program to Determine Whether a Given Number is Even or Odd Recursively
- Golang Program to Determine Recursively Whether a Given Number is Even or Odd
- Java program to check whether the given number is an Armstrong number
- Program to check whether given number is Narcissistic number or not in Python
- Check whether the given number is Euclid Number or not in Python
- Finding out the Harshad number JavaScript
- Python program to check if the given number is a Disarium Number
- Program to check whether the given number is Buzz Number or not in C++
- Write a Golang program to check whether a given number is prime number or not
- Python program to check if the given number is Happy Number
- 8085 program to check whether the given number is even or odd
- Java program to find whether given number is even or odd
- Python Program for How to check if a given number is a Fibonacci number?
- How to check if a given number is a Fibonacci number in Python Program ?
- Check whether the given number is Wagstaff prime or not in Python

Advertisements