# C Program to Print all Prime Digits in a Given Number

Write a program to print all the prime digits in a given number in c.

Example,

```Input
12345689

Output
2
3
5```

The logic is very simple. There are only 4 prime digits, that is 2,3,5 and 7. We read each digit of the input number and check if the digit is prime or not. If the digit is prime, we print the digit on the screen.

Steps

1. Let N be the input number
2. Repeat following steps while N > 0
1. Set temp = N % 10
2. N = N / 10
3. If temp == 2,3,5 or 7, then print temp

## Program to Print all Prime Digits in a Number

``````#include <stdio.h>
#include <string.h>

int main() {

int N, temp;
printf("Enter a Number: ");
scanf("%d", &N);

printf("Prime Digits: \n");

// reading each digit of N
while (N > 0) {

temp = N % 10; 	// storing the right-most digit of N in temp
// if N is 345 then temp will store 5

N = N / 10;		// removing right-most digit from N
// if N is 345, it will become 34

if (temp == 2 || temp == 3 || temp == 5 || temp == 7) {
printf("%d\n", temp);
}

}

}

``````

Output

```Enter a Number: 123456793

Prime Digits:
3
7
5
3
2```

## Print all Prime Digits in a Number without Repetition

The problem with the above program is that if a prime digit appears more than once in the integer, then it is printed multiple times.

But what if we want to display each digit only once? To do so we will use hashing. We declare an array of size 10 and fill it with all zeros. Whenever we have to print a prime digit, we will change the value of the array that corresponds to that digit to 1. Next time if we encounter the same prime digit, we can use the hash to check if the number has already been printed or not.

``````#include <stdio.h>
#include <string.h>

int main() {

int N, temp;
int hash;
// declare array of size 10 with all zeros

printf("Enter a Number: ");
scanf("%d", &N);

printf("Prime Digits: \n");
while (N > 0) {

temp = N % 10;
N = N / 10;

if ((temp == 2 || temp == 3 || temp == 5 || temp == 7) && hash[temp] == 0) {
printf("%d\n", temp);
hash[temp] = 1;
}

}

}
``````

Output

```Enter a Number: 123456793
Prime Digits:
3
7
5```

Read