📜  找到给定数字 N 的所有数字,它们是 N 的因数

📅  最后修改于: 2022-05-13 01:56:09.811000             🧑  作者: Mango

找到给定数字 N 的所有数字,它们是 N 的因数

给定一个数字N,任务是打印N中除N的数字。

例子:

方法:这个想法是遍历N的所有数字并检查该数字是否能整除N。请按照以下步骤解决问题:

  • 将变量tem初始化为N。
  • 遍历 while 循环直到tem不等于0并执行以下任务:
    • 将变量rem初始化为tem%10。
    • 如果n%rem等于0 ,则打印rem作为答案。
    • 除以10。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function that will print all the factors
void printDigitFactors(int n)
{
 
    // Storing n into a temporary variable
    int tem = n;
 
    // To store current digit
    int rem;
 
    // Iterating over all the digits
    while (tem != 0) {
 
        // Taking last digit
        int rem = tem % 10;
        if (n % rem == 0) {
            cout << rem << ' ';
        }
 
        // Removing last digit
        tem /= 10;
    }
}
 
// Driver Code:
int main()
{
    int N = 234;
 
    printDigitFactors(N);
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function that will print all the factors
  static void printDigitFactors(int n)
  {
 
    // Storing n into a temporary variable
    int tem = n;
 
    // To store current digit
    int rem;
 
    // Iterating over all the digits
    while (tem != 0) {
 
      // Taking last digit
      rem = tem % 10;
      if (n % rem == 0) {
        System.out.print(rem + " ");
      }
 
      // Removing last digit
      tem /= 10;
    }
  }
 
  public static void main (String[] args) {
 
    int N = 234;
    printDigitFactors(N);
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python code for the above approach
 
# Function that will print all the factors
def printDigitFactors(n):
 
    # Storing n into a temporary variable
    tem = n;
 
    # To store current digit
    rem = None
 
    # Iterating over all the digits
    while (tem != 0):
 
        # Taking last digit
        rem = tem % 10;
        if (n % rem == 0):
            print(rem, end= ' ');
 
        # Removing last digit
        tem = tem // 10
     
# Driver Code:
N = 234;
printDigitFactors(N);
 
  # This code is contributed by gfgking


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function that will print all the factors
  static void printDigitFactors(int n)
  {
 
    // Storing n into a temporary variable
    int tem = n;
 
    // To store current digit
    int rem = 0;
 
    // Iterating over all the digits
    while (tem != 0) {
 
      // Taking last digit
      rem = tem % 10;
      if (n % rem == 0) {
        Console.Write(rem + " ");
      }
 
      // Removing last digit
      tem /= 10;
    }
  }
 
  // Driver Code:
  public static void Main()
  {
    int N = 234;
 
    printDigitFactors(N);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
3 2 

时间复杂度: O(K),其中 K 是 N 中的位数
辅助空间: O(1)。