- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

#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

- Related Articles
- Program to find sum of prime numbers between 1 to n in C++
- Find the Product of first N Prime Numbers in C++
- Find count of Almost Prime numbers from 1 to N in C++
- C program to display all prime numbers between 1 to N using for loop
- Find two distinct prime numbers with given product in C++
- Absolute Difference between the Product of Non-Prime numbers and Prime numbers of an Array?
- Print prime numbers from 1 to N in reverse order
- Find two distinct prime numbers with given product in C++ Program
- Program to find Prime Numbers Between given Interval in C++
- Product of all prime numbers in an Array in C++
- How to Display all Prime Numbers from 1 to N in Golang?
- Swift Program to Display All Prime Numbers from 1 to N
- Java Program to Display All Prime Numbers from 1 to N
- Kotlin Program to Display All Prime Numbers from 1 to N
- Haskell program to display all prime numbers from 1 to n

Advertisements