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.