Prime Number Calculator With Limits
The user may enter the upper limit ( <= 100 )
and lower limit ( >= 100 ) -- furthermore,
the lower limit may not exceed the upper limit
On a second incorrect entry terminate program
Output user limits before the prime list
Output the prime numbers ( in tabular form )
The total number of prime numbers
The total number of non-prime numbers
The user may enter negative numbers.
The are treated the same as positive numbers.
1, 0, + 1 are not considered prime numbers and you program should take this into account.
You are to use nested for loops (2 of them -- 1 outside loop and 1 inner loop)
I have the following code so far:
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int lowlim, uplim;
int total_non = 0;
int total_prime = 0;
int flag;
int main ()
{
cout<<"This program will check for prime numbers.\n";
cout<<"Please enter an upper limit lower than 100:\n";
cin>> uplim;
if (uplim>100){
cout<<"Please enter a number lower than 100.\n";
cin>>uplim;
if (uplim>100) {
cout<<"You entered an invalid number again - goodbye!\n";
exit(0); }}
cout<<"Please enter a lower limit which is lower than the upper limit.\n";
cin >> lowlim;
if (lowlim<0){lowlim=-(lowlim);
if (lowlim>uplim) {
cout<<"Please enter a number lower than the upper limit.\n";
cin>>lowlim;
if (lowlim>uplim){
exit(1); } }}
else if (lowlim>uplim) {
cout<<"Please enter a number lower than the upper limit.\n";
cin>>lowlim;
if (lowlim>uplim){
exit(2);}}
for(int counter=lowlim; counter <= uplim; counter++)
{
flag=0 ;
for(int counter2 = 2; counter2 <= uplim; counter2++)
{
if((counter!=counter2)&&(counter % counter2 == 0))
{
flag=1 ;
total_non=total_non + 1;
break;
}
if (flag==0)
{
total_prime=total_prime+1;
cout << setw(4) << counter;
break;
} } }
return 0;
}
However, the program continuously just outputs all the odd numbers between the limits. I can't figure out why its not only printing the primes. Any help would be appreciated. Thanks.

