Answer to Question #293951 in C for Prathamesh

Question #293951

Take input of your registration number(19120856) in the University. Write a program to print all the prime numbers digits in your registration number. 


1
Expert's answer
2022-02-06T17:17:45-0500
#include <iostream>
using namespace std;

bool isPrime(int n) {
    if ( n <= 1) 
        return false;

    int i=2;
    while (i*i <= n) {
        if (n%i  == 0)
            return false;
        i++;
    }
    return true;
}

int main() {
    long number;
    int d;

    cout << "Enter a registration number ";
    cin >> number;

    cout << "The prime numbers in it are: ";
    while (number > 0) {
        d = number % 10;
        number /= 10;
        if (isPrime(d)) {
            cout << d << " ";
        }
    }
}

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

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS