C Help Ask the user for a number between 3 and 100 inclusive

C++ Help!

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.

The output should look exactly like the below example. It will run through several different tests with different numbers, so nothing should be hard coded.

I am having an issue with 2 things. I think I have the check to see if it\'s a prime number incorrect and I am not sure how to fix that. I am also having an issue at the end where it prints the last number. I have that showing up as part of the string of numbers its outputting and I am not sure how to get it to stop at user input - 1 and go to the following line to print user input and if it is prime or not prime. Below is the code I currently have written and below that will be an example of the output to show it does not match.

#include <iostream>
#include <string>
using namespace std;
int main(){
   int num = 0;

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

int temp = 1;
int i;

cout << \"Testing... \";

for(i =3;i<num ;i++){

temp = temp*i;
cout << i << \", \";
}


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

if (num % 2 == 0) {
   cout << num << \" is NOT prime\";
   cout << endl;
}

else {
cout << num << \" is prime\";
cout << endl;

}

return 0;

}

example of the output to show how it is not matching the example exactly:

input

7

Your output

Please enter a number from 3 to 100: 7

Testing... 3, 4, 5, 6, 7 is prime

Expected output

Please enter a number from 3 to 100: 7

Testing... 3, 4, 5, 6

7 is prime

7

Your output

Please enter a number from 3 to 100: 7

Testing... 3, 4, 5, 6, 7 is prime

Expected output

Please enter a number from 3 to 100: 7

Testing... 3, 4, 5, 6

7 is prime

Solution

#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!\ Enter the number in required range : \";
cin >> num;
}
  
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;
}

OUTPUT:

Please enter a number from 3 to 100: 1
Please follow the directions!
Enter the number in required range : 7

Testing... 3, 4, 5, 6
7 is prime

C++ Help! 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 divisib
C++ Help! 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 divisib
C++ Help! 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 divisib

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site