Created
February 15, 2014 11:03
-
-
Save mehikmat/9017716 to your computer and use it in GitHub Desktop.
Java code to generat any first 100 prime numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hk.prime; | |
import java.util.Scanner; | |
public class First100Prime { | |
int flag; | |
int first; | |
public First100Prime() { | |
this.first=0; | |
this.first=1; | |
} | |
public boolean isPrime(int number) | |
{ | |
for(int i=2;i<number;i++) | |
{ | |
if(number%i==0) | |
return false; | |
} | |
return true; | |
} | |
public void genPrime(int howmany) | |
{ | |
while(true) | |
{ | |
if(isPrime(first)) | |
{ | |
System.out.print(first+" "); | |
++flag; | |
if(flag==howmany) | |
break; | |
} | |
++first; | |
} | |
} | |
public static void main(String[] args) { | |
System.out.println("How many Prime you want to generate\n"); | |
int n=new Scanner(System.in).nextInt(); | |
new First100Prime().genPrime(n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment