WAP to accept 2 numbers and check whether they are twin prime or not use the function name as "twin()". the function Returns 1 if it is a twin prime otherwise zero.
(in java)
Answers
Answered by
43
import java.util.*;
public class Twin_Prime
{
public static int Prime(int prime)
{
int p,flag=0;
for (p=2;p<=(prime)/2;p++)
{
if (prime%p==0)
{
flag=1;
break;
}
}
return(flag);
}
public static int twin(int n1, int n2)
{
int ft=0,t1,t2;
if(((n1-n2)!=2) && ((n2-n1)!=2))
return(ft);
t1 = Prime(n1);
t2 = Prime(n2);
if((t1==0) && (t2==0))
{
ft=1;
return(ft);
}
else
{
ft=0;
return(ft);
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a,b,tp;
System.out.print("Enter first number: ");
a = in.nextInt();
System.out.print("Enter second number: ");
b = in.nextInt();
tp = twin(a,b);
if (tp==1)
{
System.out.println(a+ " and "+b+" are twin primes.");
}
else
{
System.out.println(a+ " and "+b+" are not twin primes.");
}
}
}
public class Twin_Prime
{
public static int Prime(int prime)
{
int p,flag=0;
for (p=2;p<=(prime)/2;p++)
{
if (prime%p==0)
{
flag=1;
break;
}
}
return(flag);
}
public static int twin(int n1, int n2)
{
int ft=0,t1,t2;
if(((n1-n2)!=2) && ((n2-n1)!=2))
return(ft);
t1 = Prime(n1);
t2 = Prime(n2);
if((t1==0) && (t2==0))
{
ft=1;
return(ft);
}
else
{
ft=0;
return(ft);
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a,b,tp;
System.out.print("Enter first number: ");
a = in.nextInt();
System.out.print("Enter second number: ");
b = in.nextInt();
tp = twin(a,b);
if (tp==1)
{
System.out.println(a+ " and "+b+" are twin primes.");
}
else
{
System.out.println(a+ " and "+b+" are not twin primes.");
}
}
}
QGP:
I have used two functions:
Answered by
19
Answer:
THIS IS BY BUFFERED READER
HOPE THIS HELPS YOU:
PLEASE MARK MY ANSWERE AS THE BRAINLIEST ONE.
Attachments:
Similar questions