📜  数字的总和与乘积除以数字

📅  最后修改于: 2021-04-26 07:09:28             🧑  作者: Mango

给定正整数N。任务是找到将数字n均分的数字的总和和乘积。

例子:

Input: N = 12
Output: Sum = 3, product = 2
1 and 2 divide 12. So, their sum is 3 and product is 2.

Input: N = 1012
Output: Sum = 4, product = 2
1, 1 and 2 divide 1012.

方法:想法是找到数字n的每个数字除以模数10,然后检查是否除以n。因此,将其加到总和上并乘以乘积。请注意,数字可以为0,因此请注意这种情况。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
  
// Print the sum and product of digits
//  that divides the number.
void countDigit(int n)
{
    int temp = n, sum = 0, product = 1;
    while (temp != 0) {
  
        // Fetching each digit of the number
        int d = temp % 10;
        temp /= 10;
  
        // Checking if digit is greater than 0
        // and can divides n.
        if (d > 0 && n % d == 0) {
            sum += d;
            product *= d;
        }
    }
  
    cout << "Sum = " << sum;
    cout << "\nProduct = " << product;
}
  
// Driver code
int main()
{
    int n = 1012;
  
    countDigit(n);
    return 0;
}


Java
// Java implementation of the
// above approach
import java.lang.*;
import java.util.*;
  
class GFG
{
// Print the sum and product of 
// digits that divides the number.
static void countDigit(int n)
{
    int temp = n, sum = 0, product = 1;
    while (temp != 0)
    {
  
        // Fetching each digit of 
        // the number
        int d = temp % 10;
        temp /= 10;
  
        // Checking if digit is greater
        // than 0 and can divides n.
        if (d > 0 && n % d == 0) 
        {
            sum += d;
            product *= d;
        }
    }
  
    System.out.print("Sum = " + sum);
    System.out.print("\nProduct = " + product);
}
  
// Driver code
public static void main(String args[])
{
    int n = 1012;
  
    countDigit(n);
}
}
  
// This code is contributed
// by Akanksha Rai(Abby_akku)


Python3
# Python3 implementation of the above approach
  
# Print the sum and product of digits
# that divides the number.
def countDigit(n):
    temp = n
    sum = 0
    product = 1
    while(temp != 0):
          
        # Fetching each digit of the number
        d = temp % 10
        temp //= 10
          
        # Checking if digit is greater 
        # than 0 and can divides n.
        if(d > 0 and n % d == 0):
            sum += d
            product *= d
              
    print("Sum =", sum)
    print("Product =", product)
  
# Driver code
if __name__=='__main__':
      
    n = 1012
    countDigit(n)
  
# This code is contributed 
# by Kirti_Mangal


C#
// C# implementation of the 
// above approach
using System;
  
class GFG
{
// Print the sum and product of 
// digits that divides the number.
static void countDigit(int n)
{
    int temp = n, sum = 0, product = 1;
    while (temp != 0)
    {
  
        // Fetching each digit of 
        // the number
        int d = temp % 10;
        temp /= 10;
  
        // Checking if digit is greater
        // than 0 and can divides n.
        if (d > 0 && n % d == 0) 
        {
            sum += d;
            product *= d;
        }
    }
  
    Console.Write("Sum = " + sum);
    Console.Write("\nProduct = " + product);
}
  
// Driver code
public static void Main()
{
    int n = 1012;
  
    countDigit(n);
}
}
  
// This code is contributed
// by Akanksha Rai(Abby_akku)


PHP
 0 && $n % $d == 0) {
            $sum += $d;
            $product *= $d;
        }
    }
  
    echo "Sum = ".$sum;
    echo "\nProduct = ".$product;
}
  
// Driver code
  
    $n = 1012;
  
    countDigit($n);
      
// This code is contributed by mits
?>


输出:
Sum = 4
Product = 2