Create a program named PrimeGenerator. Copy the isPrime method from assignment 5
ID: 3759372 • Letter: C
Question
Create a program named PrimeGenerator. Copy the isPrime method from assignment 5. In main create an array that will contain the first 200 prime numbers, find the primes and store then in the array. Then display them in the command prompt window, ten to a line. The primes must be displayed in rightaligned columns. Be sure to label your output and to comment your code.
IsPrime code:
boolean isPrime = true;
for (long count = 2; count < number; count++){
if (number % count ==0)
isPrime = false;
}
if (number <= 1)
isPrime = false;
if (number == 2)
isPrime = true;
return isPrime;
}
Explanation / Answer
#include<iostream>
using namespace std;
bool isPrime(long number)
{
bool result = true;
for (long count = 2; count < number; count++){
if (number % count ==0)
result = false;
}
if (number <= 1)
result = false;
if (number == 2)
result = true;
return result;
}
int main()
{
long arr[200];
int count = 0;
long i=2;
while(count!=200)
{
if(isPrime(i))
{
arr[count] = i;
i++;
count++;
}
else
i++;
}
for(int i=0;i<200;i++)
{
cout << arr[i] << " ";
if(i%10==0 && i!=0)
cout << " ";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.