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
Code for all prime numbers between 2 and number entered by user:
#include
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
Get step-by-step solutions from verified subject matter experts
