Computer Science, asked by coachaloke, 4 months ago

Write a program to print the sum of prime numbers from 50 to 750.​

Answers

Answered by anindyaadhikari13
0

Required Answer:-

Question:

  • Write a program to find out the sum of prime numbers from 50 to 750.

Solution:

Here comes the program.

1. In Java.

public class Sum {

 public static void main(String[] args) {

   int i, sum=0;

   for(i=50;i<=750;i++) {

     if(isPrime(i))

       sum+=i;

   }

   System.out.println("Sum: "+sum);

 }

 static boolean isPrime(int n) {

    for(int i=2;i<=n-1;i++) {

       if(n%i==0)

         return false;

    }

    return true;

 }

}

2. In Python.

def isPrime(n):

for i in range(2,n):

 if n%i==0:

  return False

return True

sum=0

for i in range(50,750+1):

if isPrime(i):

 sum+=i

print("Sum: ",sum)

Algorithm:

  1. START
  2. Iterate a loop within range 50 - 750.
  3. Check if any number is prime or not. If prime, add them up and store the sum in sum variable.
  4. Display the sum.
  5. STOP

Refer to the attachment for output ☑.

Attachments:
Similar questions