Dado un número N, la tarea es imprimir los dígitos de N que dividen a N.
Ejemplo:
Entrada: N = 234
Salida: 2 3Entrada: N = 555
Salida: 5
Enfoque: la idea es iterar sobre todos los dígitos de N y verificar si ese dígito divide a N o no. Siga los pasos a continuación para resolver el problema:
- Inicialice las variables tem como N.
- Atraviese el ciclo while hasta que tem no sea igual a 0 y realice las siguientes tareas:
- Inicialice la variable rem como tem%10.
- Si n%rem es igual a 0 , imprima rem como respuesta.
- Dividir tem por 10.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program for the above approach #include <iostream> 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
<script> // JavaScript code for the above approach // Function that will print all the factors function printDigitFactors(n) { // Storing n into a temporary variable let tem = n; // To store current digit let rem; // Iterating over all the digits while (tem != 0) { // Taking last digit let rem = tem % 10; if (n % rem == 0) { document.write(rem + ' '); } // Removing last digit tem = Math.floor(tem / 10); } } // Driver Code: let N = 234; printDigitFactors(N); // This code is contributed by Potta Lokesh </script>
Producción
3 2
Complejidad de Tiempo: O(K), donde K es el número de dígitos en N
Espacio Auxiliar: O(1).
Publicación traducida automáticamente
Artículo escrito por coder_nero y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA