Skip to content
Related Articles
Harshad (Or Niven) Number
• Difficulty Level : Easy
• Last Updated : 17 May, 2021

An integer number in base 10 which is divisible by the sum of its digits is said to be a Harshad Number. An n-harshad number is an integer number divisible by the sum of its digit in base n.
Below are the first few Harshad Numbers represented in base 10:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20………
Given a number in base 10, our task is to check if it is a Harshad Number or not.

Examples :

```Input: 3
Output: 3 is a Harshad Number

Input: 18
Output: 18 is a Harshad Number

Input: 15
Output: 15 is not a Harshad Number```

1. Extract all the digits from the number using the % operator and calculate the sum.
2. Check if the number is divisible by sum.

Below is the implementation of the above idea:

## C++

 `// C++ program to check if a number is Harshad``// Number or not.``#include ``using` `namespace` `std;``  ` `// function to check Harshad Number``bool` `checkHarshad(``int` `n)``{``    ``// calculate sum of digits``    ``int` `sum = 0;``    ``for` `(``int` `temp = n; temp > 0; temp /= 10)``        ``sum += temp % 10;``  ` `    ``// Return true if sum of digits is multiple``    ``// of n``    ``return` `(n % sum == 0);``}``  ` `// driver program to check above function``int` `main()``{``    ``checkHarshad(12) ? cout << ``"Yes\n"` `: cout << ``"No\n"``;``    ``checkHarshad(15) ? cout << ``"Yes\n"` `: cout << ``"No\n"``;``  ` `    ``return` `0;``}`

## Java

 `// Java program to check if a number is Harshad``// Number or not` `public` `class` `GFG {``    ``// method to check Harshad Number``    ``static` `boolean` `checkHarshad(``int` `n)``    ``{``        ``// calculate sum of digits``        ``int` `sum = ``0``;``        ``for` `(``int` `temp = n; temp > ``0``; temp /= ``10``)``            ``sum += temp % ``10``;` `        ``// Return true if sum of digits is multiple``        ``// of n``        ``return` `(n % sum == ``0``);``    ``}` `    ``// Driver program to test above functions``    ``public` `static` `void` `main(String[] args)``    ``{``        ``System.out.println(checkHarshad(``12``) ? ``"Yes"` `: ``"No"``);``        ``System.out.println(checkHarshad(``15``) ? ``"Yes"` `: ``"No"``);``    ``}``}`

## Python

 `# Python program to check``# if a number is Harshad``# Number or not.` `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` `# Driver Code``if``(checkHarshad(``12``)) : ``print``(``"Yes"``)``else` `: ``print` `(``"No"``)` `if` `(checkHarshad(``15``)) : ``print``(``"Yes"``)``else` `: ``print` `(``"No"``)``    ` `# This code is contributed``# by Nikita Tiwari`

## C#

 `// C# program to check if a number is Harshad``// Number or not``using` `System;` `public` `class` `GFG {` `    ``// method to check Harshad Number``    ``static` `bool` `checkHarshad(``int` `n)``    ``{` `        ``// calculate sum of digits``        ``int` `sum = 0;``        ``for` `(``int` `temp = n; temp > 0; temp /= 10)``            ``sum += temp % 10;` `        ``// Return true if sum of digits is``        ``// multiple of n``        ``return` `(n % sum == 0);``    ``}` `    ``// Driver program to test above functions``    ``public` `static` `void` `Main()``    ``{``        ``Console.WriteLine(checkHarshad(12) ? ``"Yes"` `: ``"No"``);` `        ``Console.WriteLine(checkHarshad(15) ? ``"Yes"` `: ``"No"``);``    ``}``}` `// This code is contributed by vt_m.`

## PHP

 ` 0;``                     ``\$temp` `/= 10)``        ``\$sum` `+= ``\$temp` `% 10;` `    ``// Return true if sum of``    ``// digits is multiple of n``    ``return` `(``\$n` `% ``\$sum` `== 0);``}` `// Driver Code``\$k` `= checkHarshad(12) ? ``"Yes\n"` `: ``"No\n"``;``     ``echo``(``\$k``);``\$k` `= checkHarshad(15) ? ``"Yes\n"` `: ``"No\n"``;``     ``echo``(``\$k``);` `// This code is contributed by ajit.``?>`

## Javascript

 ``

Output :

```Yes
No```

Method #2: Using string:

• We have to convert the given number to a string by taking a new variable.
• Traverse the string, Convert each element to integer and add this to sum.
• If the number is divisible by sum then it is Harshad number.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of above approach``#include``using` `namespace` `std;` `string checkHarshad(``int` `n)``{``    ` `    ``// Converting integer to string``    ``string st = to_string(n);``    ` `    ``// Initialising sum to 0``    ``int` `sum = 0;``    ``int` `length = st.length();` `    ``// Traversing through the string``    ``for``(``char` `i : st)``    ``{``        ` `        ``// Converting character to int``        ``sum = sum + (i - ``'0'``);``    ``}``    ` `    ``// Comparing number and sum``    ``if` `(n % sum == 0)``    ``{``        ``return` `"Yes"``;``    ``}``    ``else``    ``{``        ``return` `"No"``;``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `number = 18;``    ` `    ``// Passing this number to get result function``    ``cout << checkHarshad(number) << endl;``}` `// This code is contributed by rrrtnx`

## Python3

 `# Python implementation of above approach``def` `checkHarshad(n):``  ` `    ``# Converting integer to string``    ``st ``=` `str``(n)``    ` `    ``# Initialising sum to 0``    ``sum` `=` `0``    ``length ``=` `len``(st)` `    ``# Traversing through the string``    ``for` `i ``in` `st:` `        ``# Converting character to int``        ``sum` `=` `sum` `+` `int``(i)``        ` `    ``# Comparing number and sum``    ``if` `(n ``%` `sum` `=``=` `0``):``        ``return` `"Yes"``    ``else``:``        ``return` `"No"`  `# Driver Code``number ``=` `18``# passing this number to get result function``print``(checkHarshad(number))` `# This code is contributed by vikkycirus`

## Javascript

 ``

Output:

`Yes`

Time  Complexity: O(n)

References:
https://en.wikipedia.org/wiki/Harshad_number
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up