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

## Program to check whether a number is a Harshad Number or not

Here we will discuss how to check whether a number is a Harshad number or not using C++ Programming language.

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

To find the Harshad number find and add the digits of the number. If the number is divisible by the sum then the number is a Harshad number else not a Harshad number.

For Example : 153

Sum of digits = 1 + 5 + 3

= 9

153 is divisible by 9 so 153 is a Harshad Number. ### Working

Let’s see how the code works:

• User gives an input.
• Input is stored in an int type variable say num.
• Input is copied in another variable say save
• A loop is started
• Digits of the number is found
• each digit is added to sum( initial value 0).
• num is divided by 10 i.e value of num is changed in every cycle.
• loop runs until num is greater than 0.
• save is divided by sum
• if there is no remainder then the number is a Harshad number
• otherwise number is not a Harshad number

### C++ Code

```//C++ Program
#include <iostream>
using namespace std;
//main program
int main()
{
int num,sum = 0;
cout<<"Enter number: ";
//user input
cin>>num;
int n = num;
//loop to calculate the sum of digits
while(num > 0)
{
int rem = num%10;
sum = sum + rem;
num = num/10;
}
`Enter number: 8181 is a harshad number`