Answer to Question #163263 in C++ for zain ul abdeen

Question #163263

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


1
Expert's answer
2021-02-15T14:16:19-0500
#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; 
        }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment