Skip to content

Prime Number Programe in C++

Prime Number 


 int n, i, prime;
    // Assuming that the number is prime until proven otherwise

 prime = true;

 // Prompt user input
 cout << “Please enter a number for the primeness number test: “;
 cin >> n;

 // Test for prime-ness by checking for divisibility
 // by all whole numbers from 2 to sqrt(n)
 i = 2;
 while(i <= sqrt(static_cast<double>(n))){
        if(n % i == 0){
            prime = false;
        }
         i++
    }

     if(prime){
        cout << “The number is prime.” << endl;
    }else{
        cout << “The number is not prime.”;
    }