Digits That Are Divisors of the Number

The program must accept an integer N as the input. The task is to determine and print the digits in N that are divisors of N. If no such digit exists, the program should print -1.

Boundary Condition(s):
10 <= N <= 10^9

Input Format:
The first line contains the integer N.

Output Format:
A single line contains the digits in N that are divisors of N. If no such digit exists, print -1.

Example Input/Output 1:
Input:
126

Output:
1 2 6

Explanation:
All the digits in 126 are divisors of 126. Hence the output is 1 2 6.

Example Input/Output 2:
Input:
105

Output:
1 5

Explanation:
The digits 1 and 5 are divisors of 105, but 0 is not. Hence the output is 1 5.

N = int(input().strip())
digits = [int(d) for d in str(N)]
divisors = [str(d) for d in digits if d != 0 and N % d == 0]

print(" ".join(divisors) if divisors else "-1")
#include<stdio.h>

int main() {
    int N, temp, flag = 0;
    scanf("%d", &N);
    temp = N;

    while(temp) {
        int digit = temp % 10;
        if(digit && N % digit == 0) {
            printf("%d ", digit);
            flag = 1;
        }
        temp /= 10;
    }

    if(!flag) {
        printf("-1");
    }
    return 0;
}
#include <iostream>
using namespace std;

int main() {
    int N, temp, flag = 0;
    cin >> N;
    temp = N;

    while(temp) {
        int digit = temp % 10;
        if(digit && N % digit == 0) {
            cout << digit << " ";
            flag = 1;
        }
        temp /= 10;
    }

    if(!flag) {
        cout << "-1";
    }
    return 0;
}
import java.util.Scanner;

public class DivisorDigits {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int temp = N;
        boolean flag = false;

        while(temp > 0) {
            int digit = temp % 10;
            if(digit != 0 && N % digit == 0) {
                System.out.print(digit + " ");
                flag = true;
            }
            temp /= 10;
        }

        if(!flag) {
            System.out.println("-1");
        }
    }
}
Previous Article

Count of Even Digits in Odd Positions

Next Article

Difference Between Highest and Lowest Prime Digits

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *