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

Write a program called IsPrime.cpp that generates a list of all prime numbers fr

ID: 3736511 • Letter: W

Question

Write a program called IsPrime.cpp that generates a list of all prime numbers from 1 to 100 (inclusive). In your program, implement a function isPrime () that returns a Boolean value to indicate whether or not an input number is prime. Remember that a prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6 The program MUST contain the following features: A function isPrime (int num) that returns a Boolean value of true if num is a prime number or false if num is not a prime number. In main(), you must use your isPrime () function to generate a list of all prime numbers between 0 and 100 (inclusive) Hint: The mod operator (%) will tell you whether a number is evenly divisible by another number! A sample run of the program is shown below. The prime numbers between 1 and 100 are: 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 Press any key to continue . . -

Explanation / Answer

#include <iostream>
using namespace std;

// function taking an integer parameter
bool isPrime(int n)
{
// looping from 2 to n-1 and if any is divisible, returning false immediately
for(int i=2; i<n; i++)
{
if(n%i == 0)
return false;
}
  
// if it has come here, then no number is divisble
return true;
}

int main() {
cout << "The prime numbers between 1 and 100 are:" << endl;
  
// looping from 1 to 100 and printing primes
for(int i=1; i<=100; i++)
{
if(isPrime(i))
cout << i << " ";
}
}

/*OUTPUT
The prime numbers between 1 and 100 are:
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97  
*/

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