User enters two numbers, N1, and N2.
Write a program that determine if the numbers are twin primes.
Definition: Twin primes are two prime numbers separated by 2. For example, 3 and 5 are both prime numbers and separated by 2. Another example is 17, 19.
#include<iostream>
using namespace std;
//FUNCTION TO CHECK WHETHER NUMBER IS PRIME OR NOT
int isPrime(int n)
{
int i;
int flag = 0;
for(i = 2; i < n; i++)
{
if(n % i == 0)
{
flag = 1;
break;
}
}
if(flag == 1)
return 0;
else
return 1;
}
int main()
{
int n1, n2;
//TAKING INPUT FROM USER
cout<<"Enter two numbers \n";
cin>>n1>>n2;
//IF BOTH NUMBERS ARE PRIME
if(isPrime(n1) == 1 && isPrime(n2) == 1)
{
//IF THE DIFFERENCE BETWEEN NUMBERS IS 2
if((n1 - n2) == 2 || (n1 - n2) == -2)
cout<<"\nThe entered numbers are twin primes\n";
//IF THE DIFFERENCE BETWEEN NUMBERS IS NOT 2
else
cout<<"\nThe entered numbers are not twin primes\n";
}
//IF BOTH NUMBERS OR ONE OF THEM IS NOT PRIME
else
{
cout<<"\nThe entered numbers are not twin primes\n";
}
}
YOU HAVE NOT SPECIFIED THE PROGRAMMING LANGUAGE THAT'S WHY I HAVE DONE IT IN C++.
PLEASE UPVOTE IF YOU LIKED THE ANSWER
THANK YOU!!!
Get Answers For Free
Most questions answered within 1 hours.