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

basic c++ Ask the user for a number between 3 and 100 (inclusive). Using a loop,

ID: 3573392 • Letter: B

Question

basic c++

Ask the user for a number between 3 and 100 (inclusive). Using a loop, determine if the number is prime; that is, check to see if it is evenly divisible by any number other than itself. For example:

I have the below program written, however it is a bit incorrect. If the input is divisible by any number, it should not continue to print all the numbers. So i will need a correction made. Below is an example of what I mean

Input

21

Your output

Please enter a number from 3 to 100: 21 Testing... 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

21 is NOT prime

Expected output

Please enter a number from 3 to 100: 21 Testing... 3

21 is NOT prime

here is my current program

#include <iostream>
#include <string>
using namespace std;

bool isPrime(int n)
{
if(n<=1)
return false;

bool res = true;

for(int i=2;i<n;i++)
{
if(n%i == 0)
{
res = false;
break;
}
}
return res;
}

int main(){
int num = 0;

cout << "Please enter a number from 3 to 100: ";
cin >> num;
cout << num << endl;


while ((num < 3) || (num > 100)){
cout << "Please follow the directions!";
}

int i;

cout << "Testing... ";
for(i=3;i<num ;i++){
if(i==3)
cout << i;
else
cout << ", " << i;
}
cout << endl;
if (isPrime(num) == true) {
cout << num << " is prime";
cout << endl;
}
else {
cout << num << " is NOT prime";
cout << endl;
}

return 0;
}

21

Your output

Please enter a number from 3 to 100: 21 Testing... 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

21 is NOT prime

Expected output

Please enter a number from 3 to 100: 21 Testing... 3

21 is NOT prime

here is my current program

#include <iostream>
#include <string>
using namespace std;

bool isPrime(int n)
{
if(n<=1)
return false;

bool res = true;

for(int i=2;i<n;i++)
{
if(n%i == 0)
{
res = false;
break;
}
}
return res;
}

int main(){
int num = 0;

cout << "Please enter a number from 3 to 100: ";
cin >> num;
cout << num << endl;


while ((num < 3) || (num > 100)){
cout << "Please follow the directions!";
}

int i;

cout << "Testing... ";
for(i=3;i<num ;i++){
if(i==3)
cout << i;
else
cout << ", " << i;
}
cout << endl;
if (isPrime(num) == true) {
cout << num << " is prime";
cout << endl;
}
else {
cout << num << " is NOT prime";
cout << endl;
}

return 0;
}

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;
bool isPrime(int n)
{
if(n<=1)
return false;

bool res = true;

for(int i=2;i<n;i++)
{
if(n%i == 0)
{
res = false;
break;
}
}
return res;
}
int main(){
int num = 0;
cout << "Please enter a number from 3 to 100: ";
cin >> num;
cout << num << endl;


while ((num < 3) || (num > 100))
{
cout << "Please follow the directions!";
}

int i;
cout << "Testing... ";
if (isPrime(num) == true)
{
for(i=3;i<num ;i++){
       if(i==3)
       cout << i;
       else
       cout << ", " << i;
}
cout << endl;
cout << num << " is prime";
cout << endl;
}
else
{

cout << num << " is NOT prime";
cout << endl;

}


return 0;
}

====================================================================

Output:

akshay@akshay-Inspiron-3537:~/Chegg$ g++ prime.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Please enter a number from 3 to 100: 21
21
Testing... 21 is NOT prime