Harshad (Or Niven) Number
Last Updated :
11 Sep, 2023
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++
#include <bits/stdc++.h>
using namespace std;
bool checkHarshad( int n)
{
int sum = 0;
for ( int temp = n; temp > 0; temp /= 10)
sum += temp % 10;
return (n % sum == 0);
}
int main()
{
checkHarshad(12) ? cout << "Yes\n" : cout << "No\n" ;
checkHarshad(15) ? cout << "Yes\n" : cout << "No\n" ;
return 0;
}
|
Java
public class GFG {
static boolean checkHarshad( int n)
{
int sum = 0 ;
for ( int temp = n; temp > 0 ; temp /= 10 )
sum += temp % 10 ;
return (n % sum == 0 );
}
public static void main(String[] args)
{
System.out.println(checkHarshad( 12 ) ? "Yes" : "No" );
System.out.println(checkHarshad( 15 ) ? "Yes" : "No" );
}
}
|
Python3
def checkHarshad( n ) :
sum = 0
temp = n
while temp > 0 :
sum = sum + temp % 10
temp = temp / / 10
return n % sum = = 0
if (checkHarshad( 12 )) : print ( "Yes" )
else : print ( "No" )
if (checkHarshad( 15 )) : print ( "Yes" )
else : print ( "No" )
|
C#
using System;
public class GFG {
static bool checkHarshad( int n)
{
int sum = 0;
for ( int temp = n; temp > 0; temp /= 10)
sum += temp % 10;
return (n % sum == 0);
}
public static void Main()
{
Console.WriteLine(checkHarshad(12) ? "Yes" : "No" );
Console.WriteLine(checkHarshad(15) ? "Yes" : "No" );
}
}
|
PHP
<?php
function checkHarshad( $n )
{
$sum = 0;
for ( $temp = $n ; $temp > 0;
$temp /= 10)
$sum += $temp % 10;
return ( $n % $sum == 0);
}
$k = checkHarshad(12) ? "Yes\n" : "No\n" ;
echo ( $k );
$k = checkHarshad(15) ? "Yes\n" : "No\n" ;
echo ( $k );
?>
|
Javascript
<script>
function checkHarshad(n)
{
let sum = 0;
for (let temp = n; temp > 0; temp = parseInt(temp / 10, 10))
sum += temp % 10;
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++
#include<bits/stdc++.h>
using namespace std;
string checkHarshad( int n)
{
string st = to_string(n);
int sum = 0;
int length = st.length();
for ( char i : st)
{
sum = sum + (i - '0' );
}
if (n % sum == 0)
{
return "Yes" ;
}
else
{
return "No" ;
}
}
int main()
{
int number = 18;
cout << checkHarshad(number) << endl;
}
|
Java
import java.io.*;
class GFG
{
static String checkHarshad( int n)
{
String st = Integer.toString(n);
int sum = 0 ;
int length=st.length();
for ( int i = 0 ; i < length; i++){
sum += st.charAt(i)- '0' ;
}
if (n % sum == 0 ){
return "YES" ;
}
else {
return "NO" ;
}
}
public static void main(String args[]){
int number = 18 ;
System.out.println(checkHarshad(number));
}
}
|
Python3
def checkHarshad(n):
st = str (n)
sum = 0
length = len (st)
for i in st:
sum = sum + int (i)
if (n % sum = = 0 ):
return "Yes"
else :
return "No"
number = 18
print (checkHarshad(number))
|
C#
using System;
class GFG {
static String checkHarshad( int n)
{
String st = n.ToString();
int sum = 0;
int length = st.Length;
foreach ( char i in st)
{
sum = sum + (i - '0' );
}
if (n % sum == 0) {
return "Yes" ;
}
else {
return "No" ;
}
}
public static void Main()
{
int number = 18;
Console.WriteLine(checkHarshad(number));
}
}
|
Javascript
<script>
function checkHarshad(n){
let st = String(n)
let sum = 0
let length = st.length
for (i in st){
sum = sum + parseInt(i)
}
if (n % sum == 0){
return "Yes"
}
else {
return "No"
}
}
let number = 18
document.write(checkHarshad(number))
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
References:
https://en.wikipedia.org/wiki/Harshad_number
Share your thoughts in the comments
Please Login to comment...