The Artima Developer Community
Sponsored Link

Java Answers Forum
Can anyone help me with this problem??

3 replies on 1 page. Most recent reply: Aug 24, 2002 5:17 AM by windels

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
windels

Posts: 3
Nickname: willy
Registered: Aug, 2002

Can anyone help me with this problem?? Posted: Aug 22, 2002 8:22 AM
Reply to this message Reply
Advertisement
I want to make a program that gives me the first 50 prime numbers
( not the prime numbers until number 50),
then I have to put them in an array with length 50.
Is there some kind of method for this or is it only working with for
and if statements.

Please help me because if have a test tomorrow!!
thx


thomas

Posts: 42
Nickname: turbomanic
Registered: Jul, 2002

Re: Can anyone help me with this problem?? Posted: Aug 22, 2002 2:22 PM
Reply to this message Reply
public class Prime{
public static void main(String [] args){
int [] prime=new int[50];
boolean isPrime=false;
int x=1;
int counter=1;
prime[0]=1;
while(counter<50){
x=x+2;
for(int i=2;i<x;i++){
if((x%i)==0){
isPrime=false;
i=x;
}//end of if
else{
isPrime=true;
}//end of else
}//end of for
if(isPrime) prime[counter++]=x;
}//end of while
for(int j=0;j<50;j++)
System.out.print(prime[j]+" ");
}//end of main
}//end of class

//THIS IS THE ONLY WAY I CAN THINK OFF BUT
//COULD BE EASIER CHECK JAVA.MATH CLASS.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Can anyone help me with this problem?? Posted: Aug 22, 2002 5:14 PM
Reply to this message Reply
Hi,

There is a well written article on generating prime numbers at:
http://developer.java.sun.com/developer/JDCTechTips/2002/tt0806.html?s=03&w=32

The newsletter at Core Java Technologies Tech Tips is very nice also. Always something there to learn.

windels

Posts: 3
Nickname: willy
Registered: Aug, 2002

Re: Can anyone help me with this problem?? Posted: Aug 24, 2002 5:17 AM
Reply to this message Reply
Thank you very much for your help!!
I think I had a good test!

Flat View: This topic has 3 replies on 1 page
Topic: Can anyone explain how to send doc to printer using swing? Previous Topic   Next Topic Topic: help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use