Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Prime Number Calculator With Limits

Hello, my assignment for my c++ class is as follows:

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.
[2837 byte] By [ledjon] at [2007-11-11 9:51:13]
# 1 Re: Prime Number Calculator With Limits
i think its simply logic error. it looks like you test
if(flag)
inside the inner for loop, this should be just inside the outer loop, I think.

in other news, you only have to test to sqrt(max) + 1 not the full range and you only have to check odd numbers after 2, which can be treated special. This still is not the optimal prime finder, but it will help your approach a lot.
jonnin at 2007-11-11 20:59:53 >
# 2 Re: Prime Number Calculator With Limits
just chk by keeping
if (flag==0)
{
total_prime=total_prime+1;
cout << setw(4) << counter;
}
outside the inner for loop
(ie just before the ending bracket of outer for loop)
[flag should be set to 0 & counter should be displayed only if the condition (counter % counter2 == 0) is true for all the numbers in the inner for loop
if you chk the flag in the inner for loop, for every num that u chk for the 1st time in the inner for loop, if is not divisible by 2, flag remains zero,followed by break which exits from the inner loop, hence you get all odd numbers]
speed at 2007-11-11 21:00:53 >
# 3 Re: Prime Number Calculator With Limits
Ahhh, that worked guys, thanks. :)
ledjon at 2007-11-11 21:01:59 >