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

Harshad Number in C++ 

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

For Example : 153
Sum of digits = 1 + 5 + 3 = 9

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

Algorithm:-

For input num

  1. Initialize a variable sum = 0
  2. Extract each digit of num
  3. Add each digit to sum variable
  4. If at the end num is completely divisible by sum
  5. Then its a harshad’s number
Harshad number in C++

Code in C++

Run
#include <iostream>
using namespace std;

int checkHarshad(int num){
    
    int sum = 0;
    int temp = num;
    
    while(temp != 0){
        sum = sum + temp % 10;
        temp /= 10;
    }
    
    // will return 1 if num is divisible by sum, else 0
    return num % sum == 0;
}

int main ()
{
    int n = 153;
    
    if(checkHarshad(n))
        cout << n << " is a Harshad's number";
    else
        cout << n << " is not a Harshad's number";

    return 0;
}
// Time complexity: O(N)
// Space complexity: O(1)
// Where N is the number of digits in number

Prime Course Trailer

Related Banners

Output


Enter number: 71
71 is not a harshad number

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

One comment on “C++ program to check whether a number is a Harshad Number or not”


  • Darshan

    #include
    using namespace std;
    int main()
    {
    int num;
    cout <>num;
    int sum = 0;
    int temp = num;

    while (temp != 0)
    {
    int digit = num % 10;
    sum = sum + digit;
    temp = temp / 10;
    }

    if (num % sum == 0)
    {
    cout << num << " is harshad number";
    }
    else
    {
    cout << num << " is not harshad number";
    }
    }