An integer number in base 10 which is divisible by sum of it digits is said to be a Harshad Number. An *n-harshad* number is an integer number divisible by sum of its digit in base *n*.

Below are 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 % operator and calculate the sum.

2. Check if the number is divisible by sum.

Below is implementation of above idea:

## C/C++

`// C++ program to check if a number is Harshad ` `// Number or not. ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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"` `); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// php program to check if ` `// a number is Harshad ` `// Number or not. ` ` ` `// function to check ` `// Harshad Number ` `function` `checkHarshad(` `$n` `) ` `{ ` ` ` `// calculate sum of digits ` ` ` `$sum` `= 0; ` ` ` `for` `(` `$temp` `= ` `$n` `; ` `$temp` `> 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. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

Yes No

**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 contribute.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 DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.