📌  相关文章
📜  程序检查一个数字是否可被其数字之和整除

📅  最后修改于: 2021-04-29 16:30:38             🧑  作者: Mango

给定一个整数N,任务是检查该数字是否可被其数字的总和整除。如果可以整除,则打印“是”,否则打印“否”。

例子:

Input: N = 12 
Output: YES
Explanation: 
    As sum of digits of 12 = 1 + 2 = 3
    and 12 is divisible by 3 
    So the output is YES

Input: N = 123
Output: NO

方法:解决该问题的想法是提取数字并将其相加。然后检查该数字是否可被其位数相除。如果它是可分割的,则打印YES,否则打印NO。
下面是上述方法的实现:

执行:

C++
// C++ implementation of above approach
 
#include 
using namespace std;
 
// Function to check
// if the given number is divisible
// by sum of its digits
string isDivisible(long long int n)
{
    long long int temp = n;
 
    // Find sum of digits
    int sum = 0;
    while (n) {
        int k = n % 10;
        sum += k;
        n /= 10;
    }
 
    // check if sum of digits divides n
    if (temp % sum == 0)
        return "YES";
 
    return "NO";
}
 
// Driver Code
int main()
{
    long long int n = 123;
 
    cout << isDivisible(n);
 
    return 0;
}


Java
// Java implementation of above approach
class GFG
{
 
    // Function to check if the
    // given number is divisible
    // by sum of its digits
    static String isDivisible(long n)
    {
        long temp = n;
     
        // Find sum of digits
        int sum = 0;
        while (n != 0)
        {
            int k = (int) n % 10;
            sum += k;
            n /= 10;
        }
     
        // check if sum of digits divides n
        if (temp % sum == 0)
            return "YES";
     
        return "NO";
    }
     
    // Driver Code
    public static void main(String []args)
    {
        long n = 123;
        System.out.println(isDivisible(n));
    }
}
 
// This code is contributed by Ryuga


Python3
# Python 3 implementation of above approach
 
# Function to check if the given number
# is divisible by sum of its digits
def isDivisible(n):
 
    temp = n
 
    # Find sum of digits
    sum = 0;
    while (n):
     
        k = n % 10;
        sum += k;
        n /= 10;
     
    # check if sum of digits divides n
    if (temp % sum == 0):
        return "YES";
 
    return "NO";
 
# Driver Code
n = 123;
 
print(isDivisible(n));
 
# This code is contributed by
# Akanksha Rai


C#
// C# implementation of above approach
using System;
 
class GFG
{
 
    // Function to check if the
    // given number is divisible
    // by sum of its digits
    static String isDivisible(long n)
    {
        long temp = n;
     
        // Find sum of digits
        int sum = 0;
        while (n != 0)
        {
            int k = (int) n % 10;
            sum += k;
            n /= 10;
        }
     
        // check if sum of digits divides n
        if (temp % sum == 0)
            return "YES";
     
        return "NO";
    }
     
    // Driver Code
    public static void Main()
    {
        long n = 123;
        Console.WriteLine(isDivisible(n));
    }
}
 
// This code is contributed by anuj_67..


PHP


Javascript


Python3
# Python implementation of above approach
def getResult(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
n = 123
# passing this number to get result function
print(getResult(n))
 
# this code is contributed by vikkycirus


输出:
NO

方法2:使用字符串:

  • 通过使用新变量将给定数字转换为字符串。
  • 遍历字符串,将每个元素转换为整数并将其加和。
  • 如果该数字可被总和整除,则打印是
  • 其他打印编号

下面是上述方法的实现:

Python3

# Python implementation of above approach
def getResult(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
n = 123
# passing this number to get result function
print(getResult(n))
 
# this code is contributed by vikkycirus

输出:

No

时间复杂度:O(N)