Question: in c ++ In the previous lab, you wrote a console application that computes and displays all the prime numbers inclusively between 2 and a

in c ++

In the previous lab, you wrote a console application that computes and displays all the prime numbers inclusively between 2 and a number entered by the user. Write a program that has the same inputs and outputs as this previous exercise, but this time use the is_prime function you developed in the previous problem to simplify your code. Specifically, replace the inner loop by a call to is_prime so that your new program uses a single loop inside the main function rather than a loop nested within another loop.

Is prime function:

Primes.cpp

#include #include using namespace std; bool isPrime(int n); int main() { assert(isPrime(5) == true); assert(isPrime(7) == true); assert(isPrime(17) == true); assert(isPrime(61) == true); assert(isPrime(15) == false); assert(isPrime(25) == false); assert(isPrime(55) == false); return 0; } bool isPrime(int n){ for (int f = 2; f <= n / 2; f++) { if (n % f == 0) { return false; } } return true; }

Code for all prime numbers between 2 and number entered by user:

#include using namespace std;

int main() { cout << "Please enter a positive interger: "; int number,i,j,check,n; cin >> number; if (number <= 0) { cout << "Enter positive Integer"; return 0; } cout << "Prime number between 2 and " << number << " is: "; for (i=2; i<=number; i++) { check = 0; for (j = 2; j<= i/2; j++) { if(i % j == 0){ check = 1; break; } } if (check==0 && number != 1) { cout << i << " "; } } return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!