• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Display numbers between 1 and N.....

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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�
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic