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
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. matlab
Write a complete C++ program asking the user to enter numbers one by one. User enters...
Write a complete C++ program asking the user to enter numbers one by one. User enters only whole numbers 0 and 10. Once the user enters 0, then the program should print sum of only odd numbers entered by user prior to 0. A valid scenario given below: Enter a number: 5 Enter a number: 3 Enter a number: 2 Enter a number: 3 Enter a number: 8 Enter a number: 0 Sum of the odd numbers you entered is...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater...
Problem 3 Write code in R or Rstudio (Programming) A prime number is an integer greater than one whose only factors are one and itself. For example, the first ten prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23 and 29. A twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes. For example, the five twin prime pairs are (3, 5),...
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).
IN C++ PLEASE. Write a program where the user enters a number and you output an...
IN C++ PLEASE. Write a program where the user enters a number and you output an unfilled square of stars. (DO NOT PROMPT THE USER, just take in a number, The only console output should be the squares). For example the program would start with console input, The user would enters 3, you would output (note 3 is the lowest number your program will be tested with) *** * * *** or, The user enters 5, you would output *****...
Write a program that finds and prints all of the prime numbers between 3 and X...
Write a program that finds and prints all of the prime numbers between 3 and X (X is input from the user). A prime number is a number such that 1 and itself are the only numbers that evenly divide it (for example, 3, 5, 7, 11, 13, 17, …). One way to solve this problem is to use a doubly nested loop (a loop inside another loop). The outer loop can iterate from 3 to N while the inner...
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the...
* Write a Java program that calculates and displays the Fibonacci numbers * First prompt the user to input a number, N * Then use a for loop to calculate and display the first N fibonocci numbers * For example, if the user enters 5, the output should be: * 1, 1, 2, 3, 5 * * if the user enters 10, the output should be: * 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
Write a program that asks the user to enter a series of numbers separated by commas....
Write a program that asks the user to enter a series of numbers separated by commas. Here is an example of valid input: 7,9,10,2,18,6 The program should calculate and print the sum of all the numbers. Sample Run java NumberSum Enter·numbers·separated·by·commas:1,2,3↵ 6↵
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++
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