Do you Know What is Harshad Number
Tags: programming, beginners, codenewbie, computerscience
Published: 2021-06-10 12:45:08

Hello👋 Guys, In this article we gonna know about an interesting concept which merely known to some of the learners out there. The Concept is about the Harshad Number. Here we will be discussing about what is a Harshad Number and how to verify if a number is Harshad Number or not.

### What is Harshad Number🔢
In mathematics, Harshad numberis basically the sum of the digits of that particular number and the number has to be divisible by the summation value. Harshad numbers in base n are also known as n-harshad numbers. Harshad numbers were defined by D. R. Kaprekar, a mathematician from India. It can also be called as Niven number.

**For example -** 1729 is a Harshad Number Because sum of its digit is (1+7+2+9=19) and 1729 is completely divisible by 19. Hence this way we can check whether a number is Harshad or Not.
![Harshad Number Example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/noxksdsaxbyhjxkqmydu.png)


The same can be applied in a programatic way where we can the suitable code which can tell the inputted number is Harshad Number or not.

### 🔍Logic to follow to come-up with the solution :
Declare the required sets of variables to use.
<ol>
<li>Take the input number from the user keyboard.</li>

<li>Assign the input number to a declared variable.</li>

<li>Apply the loop and find the last digit of the number using modulo operator.</li>
```
            i.e., n = t % 10 ;
```
<li>Now sum that digit and put the value in the sum variable.</li>
```
             i.e., sum = sum + n ;
```
<li>Now compute the remaining number using dividend operator.</li>
```
            i.e., t = t / 10 ;
``` 
<li>This will follow until the condition becomes false inside the loop.</li>

<li>Now if the number is divisible by the sum value of the digits then print harshad number else print not harshad number.</li>
```
           i.e., if( a % sum = = 0) then Harshad else not.
```
### Let’s write the required code for the problem :

**Code :**


```

 #include<iostream>
 using namespace std;
 int main()
 {
 int a,n,t,sum=0;
 cin>>a;
 t=a;
 while(t!=0)
 {
 n=t%10;
 sum=sum+n;
 t=t/10;
 }
 if(a%sum==0)
 cout<<"Harshad Number";
 else
 cout<<"Not Harshad Number";
 return 0;
 }

```

```
 Sample Input
 1729
```

```
 Sample Output
 Harshad Number
```

Hence with this following set of snippet it is easily to check a number is Harshad Number or not.

Hope with this you learned and acquired some basic knowledge of C++ Programming.

**Drop a Love ❤** if you liked👍 this post, then share 🤝this with your friends and if anything is confusing or incorrect then let me know in the comment section.

**I would love to connect with you all at [Twitter](twitter.com/mayankp4513) | [LinkedIn](https://www.linkedin.com/in/mayank-pathak4513/)**

Thanks from my side, this is Mayank, keep learning and exploring !!

If you liked the article please consider [Buying me a coffee](https://www.buymeacoffee.com/mayankpathak)

<a href="https://www.buymeacoffee.com/mayankpathak" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-blue.png" alt="Buy Me A Coffee" style="height: 60px !important;width: 217px !important;" ></a>

Meet you in the next article......till than see ya🤚