Write a program that takes an integer input from user and print all the possible prime numbers in that number. For example: 7523
7
5
2
3
23
523
7523
#include<iostream>
#include<cmath>
using namespace std;
/*
function to check if a passed number is
a prime or not
returns true if prime
else false
*/
bool isPrime(int n){
if(n==1)
return false;
if(n==2)
return true;
for(int i=2;i<=n/2;i++){
if(n%i==0)
return false;
}
return true;
}
/*
main program
*/
int main(){
int n;
/* prompting user for a number */
cout<<"Enter a number: ";
/* taking user input */
cin>>n;
int a = 10;
int i = 0;
/*
at first the all possible one digit number will be
checked and printed if its prime
so dividing the number by 10 and checking the remainder
*/
while(a%n!=0){
int b = n;
while(b>pow(10,i)){
if(isPrime(b%a))
cout<<b%a<<"\n";
b/=a;
}
a*=10; // then multiply the dividend by 10
i+=1;
}
}
Comments
Leave a comment