Javatpoint Logo
Javatpoint Logo

Program to determine whether a given number is a Harshad number

Explanation

In this program, we need to determine whether given is Harshad or not.

Harshad number

A number is said to be the Harshad number if it is divisible by the sum of its digit.

For example, if number is 156, then sum of its digit will be 1 + 5 + 6 = 12. Since 156 is divisible by 12. So, 156 is a Harshad number.

Some of the other examples of Harshad number are 8, 54, 120, etc.

To find whether the given number is a Harshad number or not, calculate the sum of the digit of the number then, check whether the given number is divisible by the sum of its digit. If yes, then given number is a Harshad number.

Algorithm

  1. Define and initialize variable num.
  2. Make a copy of the num by storing the value of num in variable n.
  3. If num is greater than 0, then calculate the remainder by dividing num with 10.
  4. Add rem to a variable sum.
  5. Divide num by 10.
  6. Repeat the steps from 3 to 5, to calculate the sum of all digits present in a given number.
  7. If n (copy of original number) is divisible by the sum, then given number is a Harshad number. Else, given number is not a Harshad number.

Solution

Python

Output:

 156 is a harshad number

C

Output:

156 is a harshad number

JAVA

Output:

156 is a harshad number

C#

Output:

156 is a harshad number

PHP

Output:

156 is a harshad number

Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA