Prime Number Calculator With Limits


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Prime Number Calculator With Limits

  1. #1
    Join Date
    Nov 2006
    Posts
    4

    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.
    Last edited by ledjon; 11-15-2006 at 06:59 PM.

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    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.

  3. #3
    Join Date
    Oct 2006
    Posts
    36
    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]

  4. #4
    Join Date
    Nov 2006
    Posts
    4
    Ahhh, that worked guys, thanks. :)

Similar Threads

  1. Creating Prime Number command
    By 999_madcows in forum Java
    Replies: 5
    Last Post: 08-14-2006, 08:50 AM
  2. Prime number program to make go faster
    By divagoddess in forum C++
    Replies: 12
    Last Post: 05-07-2006, 10:55 PM
  3. Urgent
    By George in forum oracle.general
    Replies: 0
    Last Post: 04-01-2003, 12:02 AM
  4. Help
    By George in forum oracle.general
    Replies: 0
    Last Post: 03-31-2003, 11:57 PM
  5. Help
    By George in forum oracle.general
    Replies: 0
    Last Post: 03-31-2003, 11:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center