answersLogoWhite

0

The easiest way is to use a prime sieve (Google Sieve of Eratosthenes).

Here is pseudo-code for the algorithm.

- create a boolean array of size n

- for every true index 2 through n

- - keep the index as true, mark all multiples of the index as false.

So, for example, if n=10

start with 2, keep 2 true. Mark 4,6,8,10 as false.

next is 3, keep it true. mark 6,9 as false (6 was already false).

next is 4, it is false, skip it.

next is 5, keep it true. mark 10 as false (it was already false).

6 is false, skip it

7 is true, keep it true. the next multiple of 7 is greater than 10

8,9, 10 are all false.

you are done - the values marked true (1,2, 3,5,7) are your primes.

User Avatar

Wiki User

13y ago

Still curious? Ask our experts.

Chat with our AI personalities

ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
SteveSteve
Knowledge is a journey, you know? We'll get there.
Chat with Steve
TaigaTaiga
Every great hero faces trials, and you—yes, YOU—are no exception!
Chat with Taiga
More answers

#include

using std::cin;
using std::cout;
using std::endl;

int main()
{
int number = 0;
cout << endl << "Enter a number: ";
cin >> number;

for (int i = 0; i <= number; i++)
{
if (i % 2 == 0 && i != 0)
{
cout << endl << "Even number: " << i;
}
}

cout << endl;
system("PAUSE");
return 0;
}

User Avatar

Wiki User

15y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Java program that prints prime numbers between 1 to n?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What BASIC program can compute and display all prime numbers from 1 to 40?

PRINT 2,3,5,7,11,13,17,19,23,29,31,37


Write a c program to find out the prime numbers between 1 to 500?

To write a C program to find prime numbers between 1 to 500, you can use a nested loop structure. In the outer loop, iterate from 2 to 500, and in the inner loop, check if the number is divisible by any number from 2 to the square root of the number. If it is not divisible by any number other than 1 and itself, then it is a prime number. Print out all prime numbers found within the specified range. Remember to include necessary header files, such as &lt;stdio.h&gt;, and use appropriate logic to implement the program efficiently.


C program to find prime number?

I am providing a succinct and easy to understand version of the program. I have run it in 3-4 compilers and it works perfect. Mind you, you should not enter a number more than 2147483647 (which is the largest number a variable can process in C!). If you do, no problem, but it will display all numbers above it, including the even numbers to be prime. So here you are:#include#includemain(){long int a,b,c;printf("Enter the number: ");scanf("%ld",&a);for (b=2;b


How do you write a program to read a value and display all prime numbers up to the value?

#include#includebool is_prime(unsigned num) {unsigned max, factor;if (num


How do you print non-prime numbers in java?

Loop through some numbers - for example, 2 through 100 - and check each one whether it is a prime number (write a second loop to test whether it is divisible by any number between 2 and the number minus 1). If, in this second loop, you find a factor that is greater than 1 and less than the number, it is not a prime, and you can print it out.