In: Computer Science
(Prime Numbers) An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. Write pseudocode and function called isPrime that receives an integer and determines whether the integer is prime or not. Write a test program that uses isPrime to determine and print all the prime numbers between 1 and 1000. Display 10 numbers per line. Twin primes are a pair of prime numbers that differ by 2. For example, 3 and 5 are twin primes, as are 5 and 7, and 11 and 13. In your test program, use isPrime to determine and print all twin primes less than 1000. Display the output as follows:
(3, 5)
(5, 7)
…
FUNCTION
bool isPrime(int num)
{ int p=0;
for(int i = 2; i <= sqrt(num); i+=1)
if(num % i == 0)
p=1;
if(p==1)
return false;
else
return true;
}
Pseudocode
SOURCE CODE
#include <iostream>
#include<math.h>
using namespace std;
bool isPrime(int num)
{ int p=0;
for(int i = 2; i <= sqrt(num); i+=1)
if(num % i == 0)
p=1;
if(p==1)
return false;
else
return true;
}
int main()
{
bool flag;
int i;
for(i=2;i<1000;i++)
{
flag = isPrime(i);
if (flag == true )
{
flag = isPrime(i+2);
if (flag == true )
cout << "("<<i<<", "<< i+2<<
")\n";
}
}
return 0;
}
OUTPUT
(3, 5)
(5, 7)
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)
(101, 103)
(107, 109)
(137, 139)
(149, 151)
(179, 181)
(191, 193)
(197, 199)
(227, 229)
(239, 241)
(269, 271)
(281, 283)
(311, 313)
(347, 349)
(419, 421)
(431, 433)
(461, 463)
(521, 523)
(569, 571)
(599, 601)
(617, 619)
(641, 643)
(659, 661)
(809, 811)
(821, 823)
(827, 829)
(857, 859)
(881, 883)
SCREENSHOT
please give a upvote if u feel helpful.