Assume the availability of a function isprime Assume a varia
Assume the availability of a function is_prime. Assume a variable n has been associated with positive integer. Write the statements needed to find out how many prime numbers (starting with 2 and going in increasing order with successively higher primes [2, 3, 5, 7, 11, 13, ...]) can be added before exceeding n. Associate this number with the variable k.
Solution
#include <iostream>
#include <string>
using namespace std;
bool is_prime(int n)
{
if(n<=1)
return false;
bool res = true;
for(int i=2;i<n;i++)
{
if(n%i == 0)
{
res = false;
break;
}
}
return res;
}
int main(){
int n = 15;
int k = 0;
int prime = 2;
while(prime<=n)
{
if(is_prime(prime))
k = k+1;
prime++;
}
cout << \"Number of prime numbers can be added are : \" << k;
return 0;
}
OUTPUT:
Number of prime numbers can be added are : 6
