answersLogoWhite

0


Best Answer

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
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

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

This answer is:
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?

Oh, dude, you wanna find prime numbers? Alright, here's a simple C program for ya: You loop through numbers from 1 to 500, check if each number is prime by dividing it only by numbers up to its square root (efficient, right?), and if it's only divisible by 1 and itself, boom, you got a prime number. Print those bad boys out and voilà, you're done. Like, easy peasy, lemon squeezy!


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


In java using command line argument write prime number program?

/* Program to print prime numbers up to given number */ import java.util.*; public class Prime { public static void main(String args[]) { Scanner sc=new Scanner(System.in); System.out.println("Enter the number : (it prints prime numbers upto your given number)"); int n=sc.nextInt(); System.out.println("The Prime number upto "+n+" are : "); while (n&gt;=0) { int count=0; for (int i=1;i&lt;=n ;i++) { if (n%i==0) { count++; } } if (count==2) { System.out.print(n+" "); } n--; } } }

Related questions

How do you write a VBnet program to find the prime numbers between 100 to 200?

VBnet program to find the prime numbers between 100 to 200?


How many prime numbers between 1 and 8888888888888888888888888888888888888888888888?

How ma How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and How many prime numbers between 1 and ny prime numbers between 1 and


Prime numbers between 1 to 10 in microprocessor 8085?

program to find prime number in 8085 microprocessor


What is the product of the prime numbers between 29 and 30?

29 is a Prime number. There are no prime numbers between 29 and


Are there less prime numbers between 101 and 200 than between 1 and 100?

NO. There are more prime numbers between 1 and 100 than the prime numbers between 101 and 200.number of prime numbers between 1 and 100 = 25number of prime numbers between 101 and 200 = 20


Program for print prime all number from 1 to 100 in foxpro?

Prime numbers are numbers that are only divisible by themselves and the number 1. You can write a program to print all prime numbers from 1 to 100 in FoxPro.


Are there prime numbers between 114 and126?

There are no prime numbers between 114 and 126.


What are the prime numbers between 33 and 36?

There are no prime numbers between 33 and 36.


What are the prime numbers between 30 and 40?

The prime numbers between 30 and 40 are 31 and 37.The prime numbers between 30 and 40 are 31 and 37.


What are the prime numbers between 31 and 50?

The prime numbers between 31 and 50 are 37,41,43,47.


What are the prime numbers between 12 and 48?

The prime numbers between 12 and 48 are 13,17,19,23,29,31,37,41,43,47.


What are the prime numbers between 41 and54?

The prime numbers between 41 and 54 are 43,47,53.