# C++ program to check whether a number is a Harshad Number or not

## Harshad Number in C++

A Harshad number is a positive integer that is divisible by the sum of the digits of the integer. It is also called the Niven number.

```For Example : 153
Sum of digits = 1 + 5 + 3 = 9

153is divisible by 9 so 153 is a Harshad Number.
``` ## Algorithm:-

For input num

1. Initialize a variable sum = 0
2. Extract each digit of num
3. Add each digit to sum variable
4. If at the end num is completely divisible by sum
5. Then its a harshad’s number ### Code in C++

Run
```#include <iostream>
using namespace std;

int sum = 0;
int temp = num;

while(temp != 0){
sum = sum + temp % 10;
temp /= 10;
}

// will return 1 if num is divisible by sum, else 0
return num % sum == 0;
}

int main ()
{
int n = 153;

cout << n << " is a Harshad's number";
else
cout << n << " is not a Harshad's number";

return 0;
}
// Time complexity: O(N)
// Space complexity: O(1)
// Where N is the number of digits in number```

### Output

```Enter number: 71
71 is not a harshad number
```

Get PrepInsta Prime & get Access to all 150+ courses offered by PrepInsta in One Subscription