C Coding Questions With Solutions


Question :

Write a C-program to Calculate the sum of Prime numbers Upto N numbers

  
  
    int main(int argc,char *argv[])
{
    int N,i,j,sum=0,count;
    if(argc!=2)
      exit(1);
      N=atoi(argv[1]);
    for(i=2;i<=N;i++)
   {
    count=1;
      for(j=2;j<=i/2;j++)
   {
      if(i%j==0)
    {
      count=0;
      break;
    }
      }
        if(count==1)
      {
        sum=sum+i;
      }
    }
    printf("Sum of all prime numbers between 1 to %d=%d",N,sum);
    return 0;
}