-2

My intention was to count the sum of prime numbers in a certain range. Its working fine when the range is less than or equal to 8. When I take input 9 it shows output 26 while the answer is 17. My program identifying 9 as a prime and adding with the sum.

{
  for(int i=3; i*i<=n; i+=2)
  {
      if(n%i==0) return false;
  }

  return true;
}`

`int main(){

    int t,sum=0;
    cin >> t;
    for(int k = 0; k < t; k++){
        int n;
        cin >> n;
        if(n>=2) sum=2;`

       ` for(int i=3;i<=n;i+=2)
        {
            ifPrime(i);
            if(ifPrime) sum=sum+i;
        }`

      ` cout<<sum<<endl;
       sum=0;
    }

   return 0;
}
pm100
  • 48,078
  • 23
  • 82
  • 145

1 Answers1

2

my guess

this

       ifPrime(i);
        if(ifPrime) sum=sum+i;

should be

       if(ifPrime(i)) sum=sum+i;

but without seeing all the code its impossible to say

pm100
  • 48,078
  • 23
  • 82
  • 145