Question

User enters two numbers, N1, and N2. Write a program that determine if the numbers are...

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.

Homework Answers

Answer #1
#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!!!

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C++ [Program-2] Write a program that accepts an indefinite set of numbers until the user enters...
C++ [Program-2] Write a program that accepts an indefinite set of numbers until the user enters “-1”. In other words, the program keeps accepting new values until the user provides a “-1” (your program accepts all values, and discards the “-1”). When done, the program prints back to the user: (i) the sum of all numbers entered (except -1), (ii) the minimum value seen across all numbers (except -1), and (iii) the maximum value across all numbers (except -1).
Write a program that asks the user to enter an odd number and prints out a...
Write a program that asks the user to enter an odd number and prints out a triangle pattern. For example, if the user enters 5, the output is note: There is one space between the numbers in each line 1 3 5 1 3 1 If the user enters 9 the output is: 1 3 5 7 9 1 3 5 7 1 3 5 1 3 1 program language: C++
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
Write a program to find all the numbers divisible by 7 between a given range of...
Write a program to find all the numbers divisible by 7 between a given range of numbers [n1, n2] entered by the user using a FOR loop only. write C program. help
write a C++ program that display a prime numbers between 1 and 100 number. Plase print...
write a C++ program that display a prime numbers between 1 and 100 number. Plase print the remaining primes by “dots” For gexample The output should appear like The prime numbers between 1 and 100 are: 1, 2, 3, 5, 7, .........
Twin primes are two primes that differ by two for example 3 and 5 are twin...
Twin primes are two primes that differ by two for example 3 and 5 are twin primes as well 11 and 13, a prime triplet is there primes that differ by 2 for example 3,5,7. Prove that 3,5,7 is the only prime triple
Use C++ Write a program that first reads in how many whole numbers the user wants...
Use C++ Write a program that first reads in how many whole numbers the user wants to sum, then reads in that many whole numbers, and finally outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the numbers just once each and the user can enter them...
Function Example: Write a Python function that receives two integer arguments and writes out their sum...
Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables. def writer(n1, n2): sum = n1 + n2 product = n1*n2 print("For the numbers", n1, "and", n2) print("the sum is", sum) print("and the product is", product) ... 1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program. 2) Create the algorithm in both...
IN PYTHON : Write a program that asks the user for a number. Write the number...
IN PYTHON : Write a program that asks the user for a number. Write the number to a file called total.txt. Then ask the user for a second number. Write the new number on line 2 with the total for both numbers next to it separated by a comma. Then ask the user for a third number and do the same. Keep asking for numbers until the person puts in a zero to terminate the program. Close the file. Be...
Write a python program that does the following: Prompts the user for three numbers in one...
Write a python program that does the following: Prompts the user for three numbers in one request. Be sure to specify the “delimiter” by which a user enters those three numbers. Divides the first number by the second number and add that result to the third number. Prints output that shows in one line the formula applied and the result of the calculation. Validate input by: •    Checking the user entered 3 values •    Appropriately checking for the following errors:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT