DEV Community

Cover image for Do you Know What is Harshad Number
Mayank Pathak
Mayank Pathak

Posted on • Originally published at

Do you Know What is Harshad Number

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

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.

  1. Take the input number from the user keyboard.
  2. Assign the input number to a declared variable.
  3. Apply the loop and find the last digit of the number using modulo operator.
  4.             i.e., n = t % 10 ;
  5. Now sum that digit and put the value in the sum variable.
  6.              i.e., sum = sum + n ;
  7. Now compute the remaining number using dividend operator.
  8.             i.e., t = t / 10 ;
  9. This will follow until the condition becomes false inside the loop.
  10. Now if the number is divisible by the sum value of the digits then print harshad number else print not harshad number.
  11.            i.e., if( a % sum = = 0) then Harshad else not.

    Let’s write the required code for the problem :

    Code :

     using namespace std;
     int main()
     int a,n,t,sum=0;
     cout<<"Harshad Number";
     cout<<"Not Harshad Number";
     return 0;
     Sample Input
     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 | LinkedIn

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

    If you liked the article please consider Buying me a coffee

    Buy Me A Coffee

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

Top comments (0)