# Find product of prime numbers between 1 to n in C++

C++Server Side ProgrammingProgramming

Suppose we have a number n. We have to find the product of prime numbers between 1 to n. So if n = 7, then output will be 210, as 2 * 3 * 5 * 7 = 210.

We will use the Sieve of Eratosthenes method to find all primes. Then calculate the product of them.

## Example

Live Demo

#include<iostream>
using namespace std;
long PrimeProds(int n) {
bool prime[n + 1];
for(int i = 0; i<=n; i++){
prime[i] = true;
}
for (int i = 2; i * i <= n; i++) {
if (prime[i] == true) {
for (int j = i * 2; j <= n; j += i)
prime[j] = false;
}
}
long product = 1;
for (int i = 2; i <= n; i++)
if (prime[i])
product *= i;
return product;
}
int main() {
int n = 8;
cout << "Product of primes up to " << n << " is: " << PrimeProds(n);
}

## Output

Product of primes up to 8 is: 210
Published on 18-Dec-2019 12:57:57