Prime number program

This is a discussion on Prime number program within the C++ Programming forums, part of the General Programming Boards category; Been a long time since I posted a problem aye! Still remember ol Prelude helping me with that termination prevention ...

  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.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,628
    To do case three correctly you must compute n primes beforehand. You might first factor out the code that does work in case two as a new function and then use it to build a table of the first 1000 primes.

    After you calculate the primes, use the table and the function together to complete cases two and three. The difference is in case two you will sum all primes up to n. The difference in case three is that you will be computing up to n terms of an arithmetic series, and to do that you need a bunch of primes ready beforehand.

    For example the sum of the first five terms is 18 = 1 + 2 + 3 + 5 + 7.
    Last edited by whiteflags; 10-02-2008 at 01:57 AM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    here is a little modified case 2 program, tested .....
    Code:
    #include<iostream.h>
    #include<conio.h>
    void main() {
     cout << "\nEnter any limit";
     int l;
     cin >> l; //The limit
     int num, sum=0, flag, i;
     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 &#37; i == 0)  {
    				 flag = 0;
    						 break;
    						 }
    				 }
    		 if(flag == 1) {
    			 cout << num << "\t";
    			 sum = sum + num;
    				 }
    	 }
    		 cout << "The sum is -> " << sum;
    		 getch();
    }
    Last edited by gvkalra; 10-02-2008 at 08:13 AM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    code 3 modified a little bit too ........
    Code:
    #include<iostream.h>
    #include<conio.h>
    void main() {
    	 cout << "\nEnter the number of terms - ";
    	 int n;
    	 cin >> n;
    	 int ctr = 0; //counter variable
    	 int num, flag, i, sum=0;
    	 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();
     }
    i hope it helped

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    SVXX, gvkalra is a chronic includer of the pre-standard <iostream.h> header and a chronic void main()er (the global main function should return an int), so think twice before copying his/her code. Oh, and your indentation is much better than gvkalra's too.

    Instead I suggest that you read through citizen's post and make use of the suggestions there.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    Quote Originally Posted by laserlight View Post
    SVXX, gvkalra is a chronic includer of the pre-standard <iostream.h> header and a chronic void main()er (the global main function should return an int), so think twice before copying his/her code. Oh, and your indentation is much better than gvkalra's too.

    Instead I suggest that you read through citizen's post and make use of the suggestions there.
    m sorry for the indentation, but i just copied the code of SVXX and modified it a bit ......... for main() & <iostream.h> , may i know what's the difference? bcoz at the level i am studying, i don't know the difference ..................... the code is working 100% fine, there is nothing wrong with the logic

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    void main() is not and has never been valid C++. Apparently there's a compiler out there that accepts it, but it's not going to work if you ever change.

    iostream.h is what <iostream> used to be back before the standard was set in ... 1998, I think? Many systems provide it so that pre-standard code doesn't break, but it shouldn't be used in any code written since 1998.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    Quote Originally Posted by tabstop View Post
    void main() is not and has never been valid C++. Apparently there's a compiler out there that accepts it, but it's not going to work if you ever change.

    iostream.h is what <iostream> used to be back before the standard was set in ... 1998, I think? Many systems provide it so that pre-standard code doesn't break, but it shouldn't be used in any code written since 1998.
    thanks for replying ..... i would now search out the details over the net .....

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    [Matt's two cents]You know what, given the fact that the standard allows an implicit return of the function main() and int is fewer letters to type than void why don't rookies just use the correct main() definition and omit a return value. Its basically the best of both worlds...[/Matt's two cents]

Popular pages Recent additions subscribe to a feed

Similar Threads

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21