Thursday, 26 October 2017

Sum of Prime Numbers upto N

#include<stdio.h>
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;
}
Contributed by Priti Lohia

No comments:

Post a Comment

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...