Mayank Pathak's Blog

Mayank Pathak's Blog

Do you Know What is Harshad Number

Do you Know What is Harshad Number

Let's check what actually Harshad Number is

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

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.

freesnippingtool.com_capture_20210424203630.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 :

  1. Declare the required sets of variables to use.

  2. Take the input number from the user keyboard.

  3. Assign the input number to a declared variable.

  4. Apply the loop and find the last digit of the number using modulo operator.

                   i.e., n = t % 10 ;
    
  5. Now sum that digit and put the value in the sum variable.
                 i.e., sum = sum + n ;
    
  6. Now compute the remaining number using dividend operator.
                i.e., t = t / 10 ;
    
  7. This will follow until the condition becomes false inside the loop.

  8. Now if the number is divisible by the sum value of the digits then print harshad number else print not harshad number.

               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 | LinkedIn

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

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

 
Share this