Computer Science, asked by aakaksha, 1 year ago

wap to find sum of all the prime numbers between 500 to 600 c++

Answers

Answered by QGP
0

Sum of Primes - C++

We are tasked with writing a program to find the Sum of Primes. We will start by creating a function if a given number is Prime or not. We will then run a loop from 500 to 600, invoke the function for every number, and add the Primes together to find the Sum.

\rule{300}{1}

#include <iostream>

#include <cmath>    //Including for sqrt() function

using namespace std;

bool isPrime(int num)   //Function to check if a number is Prime

{

   //We check divisbility by all numbers from 2 to sqrt(num)

   //If divisibility is found, number is not prime and we return false

   for(int i=2;i<=(int)sqrt(num);i++)  

   {

       if(num%i==0)

       {

           return false;

       }

   }

   //If no divisibility is found, true is returned at the end

   return true;

}

int main()

{

   int Sum = 0;    //Initilaising Sum Variable

   //Set a loop from 500 to 600

   for(int i=500;i<=600;i++)

   {

       if(isPrime(i)) //Check if number is Prime

       {

           Sum += i;   //If i is prime, add it to Sum

       }

   }

   //Display the final Sum

   cout << "The Sum of Primes between 500 and 600 is: " << Sum << endl;

   

   return 0;

}

Attachments:
Similar questions