1. 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.

2. 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.

3. 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();
}```

4. 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. 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.

6. Originally Posted by laserlight
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. 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. Originally Posted by tabstop
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. [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]