Index-1              Index-2              Home Page

Print Prime and Sum

Write a C program to print all the prime numbers between 1 and 50 and sum of all printed primes.
 

3 comments:

  1. #include
    main()
    {
    int a[50],b,c=2,sum=0;
    for(b=0;b<50;b++)
    a[b]=b+1;
    printf("\n The prime numbers between 1 to 50\n\n ");
    for(b=0;b<50;b++)
    {
    for(c=2;a[b]>c;c++)
    {
    if(a[b]%c==0)
    break;
    }
    if((a[b]==c) || (a[b]==1))
    {
    printf("%d\t",a[b]);
    sum=sum+a[b];
    }
    }

    printf("\n\nThe sum of prime numbers are : %d ",sum);
    }

    ReplyDelete
  2. #include
    main()
    {
    int j,pr=1,n,sum=0,count=1;
    for(n=1; ;n++)
    {
    for(j=2;j50)break;
    if(pr==1)
    {
    //printf("%d\n",n);
    sum+=n;
    count++;
    }
    }
    printf("the sum of 1st%d prime number is:%d\n",count-1,sum);
    }

    ReplyDelete
  3. #include
    main()
    {
    int j,pr=1,n,sum=0,count=1;
    for(n=1; ;n++)
    {
    for(j=2;j50)break;
    if(pr==1)
    {
    //printf("%d\n",n);
    sum+=n;
    count++;
    }
    }
    printf("the sum of 1st%d prime number is:%d\n",count-1,sum);
    }

    ReplyDelete