DEV Community

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

Posted on • Originally published at thecodingbro.xyz

1 2

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 :

    
     #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 !!

    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🤚

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

đź‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay