Computer Science, asked by onanefelarryadeyemi, 3 days ago

Write an algorithm to print out the sum of all the prime numbers and odd numbers between 500 to 1000 without any repetition.

Answers

Answered by kargunjan9
1

Approach:

1. For Prime Numbers:

  • Prime numbers are the numbers that are neither divisible by n nor 1.
  • Traverse each number from 500 to 1000.
  • check if these numbers are divisible by themselves.
  • if no, go on adding those numbers into a variable named sum_prime.

2. For Odd Numbers:

  • Odd numbers are numbers that are not divisible by 2.
  • Traverse each number from 500 to 1000.
  • Check if these numbers are not divisible by2.
  • If true, go on adding those numbers into a variable named sum_odd.

Implementation in C++:-

int main()

{

   int i, j;

   int count, sum_prime=0;

   int sum_odd =0;

   for(i=500;i<=1000;i++)

   {

        count=0;

        for(j=2;j<=i/2;i++){

            if(i%j==0)

            {

                count++;

                break;

             }

         }

       if(count==0)

       {

           sum_prime=sum_prime+i;

       }

   }

   cout<<"sum of all prime numbers from 500 to 1000 is"<<sum_prime;

   for(int k=500;k<=1000;k++)

   {

       if(k%2!=0)

       {

           sum_odd=sum_odd+k;

       }

    }

   cout<<"sum of all odd numbers from 500 to 1000 is"<<sum_odd;

   return 0;

}

Similar questions