Open In App

Harshad (Or Niven) Number

Last Updated : 11 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

An integer number in base 10 which is divisible by the sum of its digits is said to be a Harshad Number. An n-Harshad number is an integer number divisible by the sum of its digit in base n.
Below are the first few Harshad Numbers represented in base 10:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20………
Given a number in base 10, our task is to check if it is a Harshad Number or not.

Examples : 

Input: 3
Output: 3 is a Harshad Number

Input: 18
Output: 18 is a Harshad Number

Input: 15
Output: 15 is not a Harshad Number

1. Extract all the digits from the number using the % operator and calculate the sum. 
2. Check if the number is divisible by the sum.

Below is the implementation of the above idea:

C++




// C++ program to check if a number is Harshad 
// Number or not. 
#include <bits/stdc++.h> 
using namespace std; 
    
// function to check Harshad Number 
bool checkHarshad(int n) 
    // calculate sum of digits 
    int sum = 0; 
    for (int temp = n; temp > 0; temp /= 10) 
        sum += temp % 10; 
    
    // Return true if sum of digits is multiple 
    // of n 
    return (n % sum == 0); 
    
// driver program to check above function 
int main() 
    checkHarshad(12) ? cout << "Yes\n" : cout << "No\n"
    checkHarshad(15) ? cout << "Yes\n" : cout << "No\n"
    
    return 0; 
}


Java




// Java program to check if a number is Harshad
// Number or not
  
public class GFG {
    // method to check Harshad Number
    static boolean checkHarshad(int n)
    {
        // calculate sum of digits
        int sum = 0;
        for (int temp = n; temp > 0; temp /= 10)
            sum += temp % 10;
  
        // Return true if sum of digits is multiple
        // of n
        return (n % sum == 0);
    }
  
    // Driver program to test above functions
    public static void main(String[] args)
    {
        System.out.println(checkHarshad(12) ? "Yes" : "No");
        System.out.println(checkHarshad(15) ? "Yes" : "No");
    }
}


Python3




# Python program to check 
# if a number is Harshad 
# Number or not.
  
def checkHarshad( n ) :
    sum = 0
    temp = n
    while temp > 0 :
        sum = sum + temp % 10
        temp = temp // 10
    # Return true if sum of
    # digits is multiple of n
    return n % sum == 0
  
# Driver Code
if(checkHarshad(12)) : print("Yes")
else : print ("No")
  
if (checkHarshad(15)) : print("Yes")
else : print ("No")
      
# This code is contributed
# by Nikita Tiwari


C#




// C# program to check if a number is Harshad
// Number or not
using System;
  
public class GFG {
  
    // method to check Harshad Number
    static bool checkHarshad(int n)
    {
  
        // calculate sum of digits
        int sum = 0;
        for (int temp = n; temp > 0; temp /= 10)
            sum += temp % 10;
  
        // Return true if sum of digits is
        // multiple of n
        return (n % sum == 0);
    }
  
    // Driver program to test above functions
    public static void Main()
    {
        Console.WriteLine(checkHarshad(12) ? "Yes" : "No");
  
        Console.WriteLine(checkHarshad(15) ? "Yes" : "No");
    }
}
  
// This code is contributed by vt_m.


PHP




<?php
// php program to check if 
// a number is Harshad 
// Number or not.
  
// function to check 
// Harshad Number
function checkHarshad($n)
{
    // calculate sum of digits
    $sum = 0;
    for ($temp = $n; $temp > 0;
                     $temp /= 10)
        $sum += $temp % 10;
  
    // Return true if sum of
    // digits is multiple of n
    return ($n % $sum == 0);
}
  
// Driver Code
$k = checkHarshad(12) ? "Yes\n" : "No\n";
     echo($k);
$k = checkHarshad(15) ? "Yes\n" : "No\n";
     echo($k);
  
// This code is contributed by ajit.
?>


Javascript




<script>
    // Javascript program to check if a number is Harshad Number or not
      
    // method to check Harshad Number
    function checkHarshad(n)
    {
   
        // calculate sum of digits
        let sum = 0;
        for (let temp = n; temp > 0; temp = parseInt(temp / 10, 10))
            sum += temp % 10;
   
        // Return true if sum of digits is
        // multiple of n
        return (n % sum == 0);
    }
      
    document.write(checkHarshad(12) ? "Yes" + "</br>" : "No" + "</br>");
   
      document.write(checkHarshad(15) ? "Yes" + "</br>" : "No" + "</br>");
          
</script>


Output : 

Yes
No

Time complexity: O(log10N) for given input N
Auxiliary space: O(1)

Method #2: Using string:

  • We have to convert the given number to a string by taking a new variable.
  • Traverse the string, Convert each element to an integer and add this to the sum.
  • If the number is divisible by the sum then it is Harshad number.

Below is the implementation of the above approach:

C++




// C++ implementation of above approach
#include<bits/stdc++.h>
using namespace std;
  
string checkHarshad(int n)
{
      
    // Converting integer to string
    string st = to_string(n);
      
    // Initialising sum to 0
    int sum = 0;
    int length = st.length();
  
    // Traversing through the string
    for(char i : st)
    {
          
        // Converting character to int
        sum = sum + (i - '0');
    }
      
    // Comparing number and sum
    if (n % sum == 0)
    {
        return "Yes";
    }
    else
    {
        return "No";
    }
}
  
// Driver Code
int main()
{
    int number = 18;
      
    // Passing this number to get result function
    cout << checkHarshad(number) << endl;
}
  
// This code is contributed by rrrtnx


Java




import java.io.*;
// java code to check the given number is Harshad or not
class GFG
{
  // function to check that given number
  // is Harshad or not.
    static String checkHarshad(int n)
    {
        
      // converting the integer to string
        String st = Integer.toString(n);
        int sum = 0;
        
      // calculating total number of digits
      // in a number
        int length=st.length();
        
      // adding the all digits of a number
        for(int i = 0; i < length; i++){
            sum += st.charAt(i)-'0';
        }
        
      // checking that sum is divisior of n or not
        if(n % sum == 0){
           return "YES";
        }
        else{
            return "NO";
        }
    }
    
  // driver code
    public static void main(String args[]){
        int number = 18;
        
      // function call
        System.out.println(checkHarshad(number));
    
}
  
// This code is contributed by Machhaliya Muhammad


Python3




# Python implementation of above approach
def checkHarshad(n):
    
    # Converting integer to string
    st = str(n)
      
    # Initialising sum to 0
    sum = 0
    length = len(st)
  
    # Traversing through the string
    for i in st:
  
        # Converting character to int
        sum = sum + int(i)
          
    # Comparing number and sum
    if (n % sum == 0):
        return "Yes"
    else:
        return "No"
  
  
# Driver Code
number = 18
# passing this number to get result function
print(checkHarshad(number))
  
# This code is contributed by vikkycirus


C#




// C# program to find the radii
// of the three tangent circles
// of equal radius when the radius
// of the circumscribed circle is given
using System;
  
class GFG {
    static String checkHarshad(int n)
    {
  
        // Converting integer to string
        String st = n.ToString();
  
        // Initialising sum to 0
        int sum = 0;
        int length = st.Length;
  
        // Traversing through the string
        foreach(char i in st)
        {
            // Converting character to int
            sum = sum + (i - '0');
        }
  
        // Comparing number and sum
        if (n % sum == 0) {
            return "Yes";
        }
        else {
            return "No";
        }
    }
  
    // Driver code
    public static void Main()
    {
        int number = 18;
  
        // Passing this number to get result function
        Console.WriteLine(checkHarshad(number));
    }
}
  
// This code is contributed by Nidhi goel


Javascript




<script>
// Javascript implementation of above approach
function checkHarshad(n){
  
    // Converting integer to string
    let st = String(n)
      
    // Initialising sum to 0
    let sum = 0
    let length = st.length
  
    // Traversing through the string
    for(i in st){
        // Converting character to int
        sum = sum + parseInt(i)
    }
          
    // Comparing number and sum
    if (n % sum == 0){
        return "Yes"
    }
    else{
        return "No"
    }
}
  
// Driver Code
let number = 18
// passing this number to get result function
document.write(checkHarshad(number))
  
// This code is contributed by _saurabh_jaiswal
</script>


Output

Yes

Time  Complexity: O(n)
Auxiliary Space: O(n)

References: 
https://en.wikipedia.org/wiki/Harshad_number

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads