Hello;

I am doing a practice problem to write a program that allows a user to enter a number between 1 and 999. The code MUST be cutoff at 999 and no other numbers past that should work. I have developed a code, but it allows the user to enter numbers past 999. I am wondering how I can create a cutoff code. It says I should use a looping statement to test all possible answers.

Any assistence would be greatly appreciated. Thanks in advance!

#include <iostream>
using namespace std;
main()
{
 
int a, i, g, no_prime, o=0;
i = 0;
g = 0;
no_prime= 0;
cout << "Enter a number:";
cin >> a;
cout << endl <<"The number is " << a <<endl;
for (i=2; i<=a; i++)
{
 
if (a % i == 0)
{
cout << i << " is a factor of " << a << endl;
++o;
}
}
if (o == 1)
cout << "The number " << a <<" is prime\n";
else
cout << "The number " << a <<" is not prime\n";
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.