# Display numbers between 1 and N.....

Allen Vaga

Greenhorn

Posts: 1

posted 12 years ago

Hi, I�m trying to figure out loops for this program.

I created a text field and area. In the text field the user will input a positive integer (N) and than the text area will display all the prime numbers that fall between 1 and N.

I�m very new to programming�

So I�m not totally sure how to go about this.

So in some very rough code� would something like this work?

DisplayTextArea (( k > 1) && (k < n)

while ( (k% 1 ==0) && ( k% 2 ==0) && (k%3 ==0) && (k%5 ==0) && (k% 7==0));

Where K represents the number range between 1 and N.

I�ve been working on this for two days no, consulting my book and online tutorials. I stumbled onto this site hoping for the best..

Thanks in advance�

I created a text field and area. In the text field the user will input a positive integer (N) and than the text area will display all the prime numbers that fall between 1 and N.

I�m very new to programming�

So I�m not totally sure how to go about this.

So in some very rough code� would something like this work?

DisplayTextArea (( k > 1) && (k < n)

while ( (k% 1 ==0) && ( k% 2 ==0) && (k%3 ==0) && (k%5 ==0) && (k% 7==0));

Where K represents the number range between 1 and N.

I�ve been working on this for two days no, consulting my book and online tutorials. I stumbled onto this site hoping for the best..

Thanks in advance�

MEHULecs DIKSHITecs

Greenhorn

Posts: 4

posted 12 years ago

/*

class Prime

Input

The maximum integer upto which primes have to be generated

Output

The prime integers each separated by a white space and ending with

a new line character

Algorithm

1) Instantiate a list of max integers with initial value 0

2) Start at the first number say "i" on the list

which has not yet been considered(should not start from 1)

3) Check if the value of the ith element in the list is 1

if it is 1 increment i otherwise proceed to step 4

4) Set all numbers of the list which are multiples of i to 1

Increment i and go back to step 2

*/

class Prime

{

public void computePrime(int max)

{

int [] intArr = new int[max+1];//Array of integers. For convenience we are making an array of max + 1 instead of max

for(int i = 2; i <= max; i++)

{

if (intArr[i] != 1)

{

System.out.println(i);

for(int j=2*i; j <= max; j+=i)

intArr[j] = 1;

}

}

}

public static void main(String [] args)

{

int max = 50;// Max num upto which primes have to be generated

Prime prime = new Prime();

prime.computePrime(max);

}

}

Try this and solve few errors if any

class Prime

Input

The maximum integer upto which primes have to be generated

Output

The prime integers each separated by a white space and ending with

a new line character

Algorithm

1) Instantiate a list of max integers with initial value 0

2) Start at the first number say "i" on the list

which has not yet been considered(should not start from 1)

3) Check if the value of the ith element in the list is 1

if it is 1 increment i otherwise proceed to step 4

4) Set all numbers of the list which are multiples of i to 1

Increment i and go back to step 2

*/

class Prime

{

public void computePrime(int max)

{

int [] intArr = new int[max+1];//Array of integers. For convenience we are making an array of max + 1 instead of max

for(int i = 2; i <= max; i++)

{

if (intArr[i] != 1)

{

System.out.println(i);

for(int j=2*i; j <= max; j+=i)

intArr[j] = 1;

}

}

}

public static void main(String [] args)

{

int max = 50;// Max num upto which primes have to be generated

Prime prime = new Prime();

prime.computePrime(max);

}

}

Try this and solve few errors if any