Skip to content

Instantly share code, notes, and snippets.

@mehikmat
Created February 15, 2014 11:03
Show Gist options
  • Save mehikmat/9017716 to your computer and use it in GitHub Desktop.
Save mehikmat/9017716 to your computer and use it in GitHub Desktop.
Java code to generat any first 100 prime numbers
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