-
Notifications
You must be signed in to change notification settings - Fork 0
/
NDigitPrimeNumbers.CPP
65 lines (58 loc) · 1.13 KB
/
NDigitPrimeNumbers.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;
int isPrime(int p);
int isDigPrime(int p);
int sumOfDigits(int p);
//Function for Prime numbers
void genPrimeDigNum(int n) {
int i = 1;
for (int k = 2; i<=n; k++){
//if k is prime
if (isPrime(k)){
if(isDigPrime(k)){
cout << k <<" ";
i++; //successful printDigit
}
}
}
}
int isPrime(int p) {
bool flag = 0;
int tmp = p/2;
for (int i = 2; i<=tmp; i++) {
if (p % i == 0 && p!=2) {
flag = 1;
}
}
if (flag != 0)
return 0;
else
return 1;
}
int isDigPrime(int p) {
int sum;
sum = sumOfDigits(p);
return isPrime(sum);
}
int sumOfDigits(int p){
int sum = 0, tmp;
while (p != 0) {
tmp = p % 10;
sum += tmp;
p = p / 10;
}
return sum;
}
int main()
{
//Code here
int n;
cout << "Enter the number of PrimeDigits to be printed:";
cin >> n;
if (n == 1)
cout << 2;
else {
genPrimeDigNum(n);
}
return 0;
}