Skip to content

Instantly share code, notes, and snippets.

@vajahath
Last active September 30, 2015 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vajahath/110850ff955608c35cc1 to your computer and use it in GitHub Desktop.
Save vajahath/110850ff955608c35cc1 to your computer and use it in GitHub Desktop.
Java Program to FILTER PRIMES
//to filter primes
import java.io.*;
import java.util.*;
class FilterPrime
{
int n;
FilterPrime(Scanner scan)
{
System.out.print("Enter Limit Here:");
n=scan.nextInt();
}
int Process()
{
int flag, count=0;
for(int i=3;i<=n;i++)
{
flag=0;
for(int j=2;j<=(i/2)+1;j++)
{
if(i%j==0)
{
flag++;
break;
}
}
if (flag==0)
{
System.out.print("\t"+i);
count++;
}
}
return count;
}
}
class Prime
{
public static void main(String []args)
{
Scanner scan=new Scanner(System.in);
FilterPrime obj=new FilterPrime(scan);
System.out.print("\n\n Primes in the limit "+obj.n+"are shown below:\n");
int c=obj.Process();
System.out.println("\n\n-------------\nTotal Count="+c);
}
}
//end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment