Prime
Series between a Given Range
import
java.io.*;
class Prime_Series
{
public static void main(String
args[])throws IOException
{
BufferedReader Br=new
BufferedReader(new InputStreamReader(System.in));
int n,m;
System.out.print("\tEnter a Lower Range
: ");
m=Integer.parseInt(Br.readLine());
System.out.print("\tEnter a Upper Range
: ");
n=Integer.parseInt(Br.readLine());
Prime Pr=new Prime();
System.out.print("\tThe Prime Nos. between "+m+" and
"+n+" is : ");
for(int
i=m;i<=n;i++)
{
if(Pr.isPrime(i)==1)
{
System.out.print("
"+i);
}
}
}
int isPrime(int n)
{
int i;;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
return 0;
}
}
return 1;
}
}
Output
Enter a Lower Range
: 3
Enter a Upper Range
: 25
The Prime Nos. between 3 and 25 is
: 3
5 7 11 13 17
19 23
Twin
Prime between a given range.
Twin
Prime : Twin primes are pairs of primes which differ by two. The first twin
primes are (3,5), (5,7).
import
java.io.*;
class Twin_Prime
{
public static void main(String
args[])throws IOException
{
BufferedReader Br=new
BufferedReader(new InputStreamReader(System.in));
int n,m;
System.out.print("\tEnter a Lower Range
: ");
m=Integer.parseInt(Br.readLine());
System.out.print("\tEnter
a Upper Range : ");
n=Integer.parseInt(Br.readLine());
Twin_Prime Pr=new
Twin_Prime();
System.out.print("\tThe Twin Prime Nos. between "+m+" and
"+n+" is : ");
for(int
i=m;i<=n;i++)
{
if(Pr.isPrime(i)==1
&& Pr.isPrime(i+2)==1)
{
System.out.print("("+i+","+(i+2)+") ");
}
}
}
int
isPrime(int n)
{
int i;;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
return 0;
}
}
return 1;
}
}
Output
Enter a Lower Range
: 3
Enter a Upper Range
: 30
The Twin Prime Nos. between 3 and 30
is : (3,5) (5,7) (11,13)
(17,19) (29,31)
Near
Prime of a given Composite No.
import
java.io.*;
class Near_Prime
{
public static void main(String
args[])throws IOException
{
BufferedReader Br=new
BufferedReader(new InputStreamReader(System.in));
int n,i,j;
Near_Prime Pr=new
Near_Prime();
while(true)
{
System.out.print("\tEnter a Composite No : ");
n=Integer.parseInt(Br.readLine());
if(!Pr.isPrime(n))
{
break;
}
}
for(i=n+1;;i++)
{
if(Pr.isPrime(i))
{
break;
}
}
for(j=n-1;j>1;j--)
{
if(Pr.isPrime(j))
{
break;
}
}
if(n-j<i-n)
{
System.out.print("\n\tNear Prime of "+n+" is = "+j);
}
else
{
System.out.print("\n\tNear Prime of "+n+" is = "+i);
}
}
boolean isPrime(int n)
{
int i;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
return
false;
}
}
return true;
}
}
Output
Enter a Composite No : 10
Near Prime of 10 is = 11
Enter a Composite No : 14
Near Prime of 14 is = 13
Enter a Composite No : 15
Near Prime of 15 is both = 17 and 13
No comments:
Post a Comment