This simple java program accepts a number as a limit and analyse each number. If it is prime a flag is set and it is displayed. Also it shows the total number of primes obtained within the range.
Here's the program.
PROGRAM
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
//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 |
Click here for instructions for executing the program.
---------------------------------------
If not working/incorrect execution please notify me below.
Also share your experience.
Have a nice day.
Comments
Post a Comment