- Related Questions & Answers
- 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++
- Absolute Difference between the Product of Non-Prime numbers and Prime numbers of an Array?
- Find two distinct prime numbers with given product in C++
- Print prime numbers from 1 to N in reverse order
- Find two distinct prime numbers with given product in C++ Program
- Product of all prime numbers in an Array in C++
- Program to find Prime Numbers Between given Interval in C++
- Count pairs of numbers from 1 to N with Product divisible by their Sum in C++
- Find if n can be written as product of k numbers in C++
- Program to find duplicate element from n+1 numbers ranging from 1 to n in Python
- Find maximum product of digits among numbers less than or equal to N in C++
- C/C++ Program to find Product of unique prime factors of a number?
- Sum of the first N Prime numbers

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

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.

#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); }

Product of primes up to 8 is: 210

Advertisements