Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I write a program that prompts the user to enter in a number and prints o

ID: 3634695 • Letter: H

Question

How do I write a program that prompts the user to enter in a number and prints out the total number of twin primes between 1 and that number. A prime number X is also a twin prime if the number (X + 2) is also prime. Therefore, 3 and 5 are twin primes and so are 11 and 13. 1 does not count as a prime number. Organize the program to use functions to calculate primes.

****** PLEASE COMMENT CODE ********

Output should look like this:

Enter in a positive number: 15

The number of twin primes is: 3

Run the program using 15, 100 and 1000 as inputs on the cluster

Explanation / Answer

please rate - thanks

#include <iostream>
using namespace std;
bool isPrime(int);
int main()
{int number,count=0,i;
bool prime;
cout<<"Enter a number ";
cin>>number;
for(i=2;i<=number-2;i++)      //can stop 2 before end, because 2 before end plus 2 is the end
   if(isPrime(i)&&isPrime(i+2))
          count++;
cout<<"The number of twin primes is: "<<count<<endl;
system("pause");
return 0;
}
bool isPrime(int n)
{int i;
for(i=2;i<n-1;i++)
   if(n%i==0)               //if you find a factor the number isn't prime
      return false;
return true;                // get here only if no factors
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote