i got these 2 questions wrong. can you help identify the errors? //Requires: sta
ID: 3631068 • Letter: I
Question
i got these 2 questions wrong. can you help identify the errors?//Requires: start > 0; end > 0
//Modifies: nothing
//Effects: prints out the twin primes between start and end inclusive
//if start > end, it will swap them
//if start = 1, end = 20 this prints
//(3, 5) (5, 7) (11, 13) (17, 19)
//FYI: twin primes are 2 prime numbers whose difference is 2
void printTwinPrimes(int start, int end);
void printTwinPrimes(int start, int end){
int x = 0;
if (start > end){
swap (start, end);
}
//if (start % 2 == 0){
// start += 1;
//}
while (start < end - 2){
if (isPrime(start) && isPrime(start + 2)){
cout << '(' << start << ", " << start + 2 << ") ";
}
start++;
}
}
//Requires: start > 0; end > 0
//Modifies: nothing
//Effects: prints out the cousin primes between start and end inclusive
//if start > end, it will swap them
//if start = 1, end = 25 this prints
//(3, 7) (7, 11) (13, 17) (19, 23)
//FYI: cousin primes are 2 prime numbers whose difference is 4
void printCousinPrimes(int start, int end);
void printCousinPrimes(int start, int end){
int x = 0;
if (start > end){
swap (start, end);
}
if (start % 2 == 0){
start += 1;
}
while (start < end - 4){
if (isPrime(start) && isPrime(start + 4)){
cout << '(' << start << ", " << start + 4 << ") ";
}
start++;
}
}
Explanation / Answer
#include<iostream>
#include<math.h>
using namespace std;
bool isPrime(int ab)
{
bool ans=true;
for(int a=2; a<=sqrt(ab); a++)
{
if(ab%a==0) { ans= false; break;}
}
return ans;
}
void printTwinPrimes(int start, int end){
int x = 0;
if (start > end){
swap (start, end);
}
//if (start % 2 == 0){
// start += 1;
//}
while (start < end - 2){
if (isPrime(start) && isPrime(start + 2)){
cout << '(' << start << ", " << start + 2 << ") ";
}
start++;
}
}
int main()
{
printTwinPrimes(20,100);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.