📜  Harshad(或Niven)编号

📅  最后修改于: 2021-05-04 14:21:04             🧑  作者: Mango

以10为底的整数可被其位数之和整除,称为Harshad数。的n哈沙德数是由它的数字的在基数n的整数,且数整除。
以下是以10为基数的前几个Harshad数:
1、2、3、4、5、6、7、8、9、10、12、18、20………
给定以10为底的数字,我们的任务是检查它是否为Harshad数。
例子 :

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.使用%运算符从数字中提取所有数字并计算总和。
2.检查该数字是否可被总和整除。
下面是上述想法的实现:

C++
// C++ program to check if a number is Harshad
// Number or not.
#include 
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");
    }
}


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


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


输出 :

Yes
No

方法2:使用字符串:

  • 我们必须通过采用新变量将给定数字转换为字符串。
  • 遍历字符串,将每个元素转换为整数并将其加和。
  • 如果该数字可被总和整除,则为Harshad数。

下面是上述方法的实现:

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

输出:

Yes