943,982 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 11099
  • Java RSS
Oct 11th, 2007
0

Re: find 100 first prime number

Expand Post »
THis is program in java for First 100 Prime Number as "count<100" .
public class PrimeNumber
{

public static void main(String args[])
{
int num=4;
int count=0;
System.out.println("First 100 Prime Number is: ");
System.out.println("1");
System.out.println("2");
System.out.println("3");
while(count<100)
{
num++;
if(num%2!=0)
{
int dv=3;
while((num%dv!=0)&&(dv<=((num-1)/2)))
{
dv++;
}
if(num%dv!=0)
{
count++;
System.out.println(num);
}
}
}

}
}
Last edited by jips11in; Oct 11th, 2007 at 3:57 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jips11in is offline Offline
1 posts
since Oct 2007
Oct 11th, 2007
0

Re: find 100 first prime number

And ?
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 11th, 2007
0

Re: find 100 first prime number

actually, it prints the first 102 prime numbers (if it works at all)...
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 2nd, 2009
0
Re: Re: find 100 first prime number
Java Syntax (Toggle Plain Text)
  1. public class Algarv {
  2. public static void main(String args[]) {
  3. int numbrid = 0; // Proovime kohe algusest
  4. int loendur = 0; // Loendame üles, et pärast lihtsam oleks
  5. while(loendur < 100) // Eelkontrolliga, t2idame kuni see on 100
  6. {
  7. numbrid++;
  8. if(numbrid % 2 != 0)
  9. {
  10. int abi = 3;
  11. while((numbrid % abi != 0)&&(abi <= ((numbrid - 1) / 2)))
  12. {
  13. abi++;
  14. }
  15. if(numbrid % abi != 0)
  16. {
  17. loendur++;
  18. System.out.print(numbrid+" ");
  19. System.out.println(loendur);
  20. }
  21. }
  22. }
  23. }
  24. }
Reputation Points: 18
Solved Threads: 11
Junior Poster in Training
-ordi- is offline Offline
91 posts
since Dec 2009
Dec 2nd, 2009
0
Re: Re: find 100 first prime number
1 is not technically a prime number either so it is not true to print it out as prime with two and three.
Reputation Points: 10
Solved Threads: 3
Light Poster
tjsail is offline Offline
40 posts
since Nov 2009
Dec 3rd, 2009
-1
Re: Re: find 100 first prime number
Click to Expand / Collapse  Quote originally posted by -ordi- ...
Java Syntax (Toggle Plain Text)
  1. public class Algarv {
  2. public static void main(String args[]) {
  3. int numbrid = 0; // Proovime kohe algusest
  4. int loendur = 0; // Loendame üles, et pärast lihtsam oleks
  5. while(loendur < 100) // Eelkontrolliga, t2idame kuni see on 100
  6. {
  7. numbrid++;
  8. if(numbrid % 2 != 0)
  9. {
  10. int abi = 3;
  11. while((numbrid % abi != 0)&&(abi <= ((numbrid - 1) / 2)))
  12. {
  13. abi++;
  14. }
  15. if(numbrid % abi != 0)
  16. {
  17. loendur++;
  18. System.out.print(numbrid+" ");
  19. System.out.println(loendur);
  20. }
  21. }
  22. }
  23. }
  24. }
For future reference, please have your comments in the code in English
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Mar 6th, 2010
0
Re: Re: find 100 first prime number
Java Syntax (Toggle Plain Text)
  1. //simple java program for finding prime nos of any range//
  2. import java.io.*;
  3.  
  4. class PrimeNumber {
  5. public static void main(String[] args) throws Exception{
  6. int i,j;
  7. BufferedReader bf = new BufferedReader(
  8. new InputStreamReader(System.in));
  9. System.out.println("Enter range:");
  10. int num =Integer.parseInt(bf.readLine());
  11. int num1=Integer.parseInt(bf.readLine());
  12. //int num=100;int num1=200;
  13.  
  14. System.out.println("Prime number: ");
  15. for(i=1;i<num||i<num1;i++)
  16. {
  17. for (j=num; j<i; j++){
  18. int n = i%j;
  19. if (n==0){
  20. break;
  21. }
  22. }
  23. if(i == j){
  24. System.out.print(" "+i);
  25. }
  26. }
  27. }
  28. }
Last edited by ~s.o.s~; Mar 6th, 2010 at 8:03 am. Reason: Added code tags, please learn to use them.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shruths2890 is offline Offline
3 posts
since Mar 2010
Mar 6th, 2010
-1

another program

Java Syntax (Toggle Plain Text)
  1. java syntax(Toggle plain text)
  2. import java.io.*;
  3.  
  4. class PrimeNumber {
  5. public static void main(String[] args) throws Exception{
  6. int i,j,count =0;
  7. BufferedReader bf = new BufferedReader(
  8. new InputStreamReader(System.in));
  9. System.out.println("Enter range:");
  10. int num =Integer.parseInt(bf.readLine());
  11. int num1=Integer.parseInt(bf.readLine());
  12.  
  13. System.out.println("Prime number: ");
  14.  
  15. for(i=num;i<num1;i++)
  16. {
  17. for (j=2; j<i; j++)
  18. {
  19. int n = i%2;
  20. if (n==0)
  21. {
  22.  
  23. break;
  24. }
  25. }
  26. if(i == j)
  27. {
  28. System.out.print(" "+i);
  29. }
  30. }
  31.  
  32.  
  33. }
  34. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shruths2890 is offline Offline
3 posts
since Mar 2010
Feb 8th, 2011
0
Re: Re: find 100 first prime number
import java.io.*;
class Primenumber{
public static void main(String args[])throws IOException{
InputStreamReader input= new InputStreamReader(System.in);
BufferedReader reader= new BufferedReader(input);

System.out.println("enter your first num");
int num1=Integer.parseInt(reader.readLine());
System.out.println("enter your second num");
int num2=Integer.parseInt(reader.readLine());
int count=0;//count has to outside of "for"

for(int x=num1;x<num2;x++){
for(count=2;count<x;count++){

if(x%count==0){
break;
}

}
if(x==count){
System.out.print(x+",");

}
}

}
}
//9 isn't a prime number I fixed it so that it works properly
Last edited by Brickhouse11; Feb 8th, 2011 at 8:29 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Brickhouse11 is offline Offline
1 posts
since Feb 2011
Feb 9th, 2011
0
Re: Re: find 100 first prime number
:sigh: Closing this zombie now.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: Problem in Java
Next Thread in Java Forum Timeline: how to set background color for jpanel





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC