top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

C: How to find sum of all prime numbers between two nos (both inclusive)?

+2 votes
498 views
C: How to find sum of all prime numbers between two nos (both inclusive)?
posted Sep 12, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 void main(void)
 {
     unsigned int a=100, b=500; // a and b are the two numbers...
     unsigned int i,j,s=0,is_prime;

     for(i=a;i<=b;i++)
     {
         is_prime = 1; // Assuming that current value of i is prime

         // Checking for prime
         for(j=2;j<=(i/2);j++)
         {
             if(i%j==0) // Not prime, set is_prime to 0 and break
             {
                 is_prime = 0;
                 break;
             }
         }

         if(is_prime) // If the number is prime, sum it up
         {
             s += i;
         }
     }

     printf("\n\nThe sum of the prime numbers = %d",s);
 }
answer Sep 12, 2014 by Salil Agrawal
Similar Questions
+2 votes

How to find prime numbers between given intervals? Sample code along with the complexity would be helpful?

+1 vote

Say you are given

int a[ ] = {1,2,3,4};
int b[ ] = {4,3,2,1};
int S = 5;

Now the task is to find all pairs of two number which adds up to S (One number from a and other number from b).

0 votes

For given list of numbers find out triplets with sum 0 using C?
Input : arr[] = {0, -1, 2, -3, 1}
Output : 0 -1 1
2 -3 1

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...