Thread: Prime number program

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52

    Prime number program

    Been a long time since I posted a problem aye! Still remember ol Prelude helping me with that termination prevention thingy.
    So, to the point. I'm making a prime numbers program and in two cases I do the following -:
    2. Enter a limit upto which primes will be displayed and summed.
    3. Enter 'n' for which n primes will be displayed and summed.
    I do get the display but not the sum. Can't seem to figure out whats wrong.
    Here's case 2-:
    Code:
     case 2:
                           cout << "\nEnter any limit";
                           cin >> l; //The limit
                           for(num = 2; num <= l; num++) //num begins at 2 and keeps incrementing till l
                           {
                                   flag = 1; //flag variable
                                   for(i = 2; i <= num/2; i++)
                                   {
                                         if (num % i == 0)
                                         flag = 0;
                                         break;
                                   }
                                   if(flag == 1)
                                   cout << num << "\t";
                                   sum = sum + num;
                           }
                                 
                                   cout << "The sum is -> " << sum;
                                   getch();
                                   break;
    And case 3-:
    Code:
    case 3:
                           cout << "\nEnter the number of terms - ";
                           cin >> n;
                           ctr = 0; //counter variable
                           for(num = 2; ctr < n; num++)
                           {
                                   flag = 1;
                                   for(i = 2; i <= num/2; i++)
                                   {
                                         if(num % i == 0)
                                         flag = 0;
                                         break;
                                   }
                                   if(flag == 1)
                                   cout << num << "\t";
                                   sum += num;
                                   ctr++;
                           }
                                   cout << "The sum is -> " << sum;
                                   getch();
                                   break;
    And the variables have been declared before hand.
    Last edited by SVXX; 10-02-2008 at 01:25 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 07:19 PM
  2. Largest number program
    By rebel in forum C++ Programming
    Replies: 10
    Last Post: 12-01-2005, 04:20 AM
  3. prime number program code
    By jd7joe in forum C++ Programming
    Replies: 1
    Last Post: 11-17-2005, 10:14 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Replies: 4
    Last Post: 03-09-2002, 01:22 PM