Given a number N, the task is to print the digits of N which divide N.
Example:
Input: N = 234
Output: 2 3Input: N = 555
Output: 5
Approach: The idea is to iterate over all the digits of N and check whether that digit divides N or not. Follow the steps below to solve the problem:
- Initialize the variables tem as N.
- Traverse over the while loop till tem not equals to 0 and perform the following tasks:
- Initialize the variable rem as tem%10.
- If n%rem equals 0 then print rem as the answer.
- Divide tem by 10.
Below is the implementation of the above approach.
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> |
3 2
Time Complexity: O(K), where K is the number of digits in N
Auxiliary Space: O(1).
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!