Write a program to print the sum of prime numbers from 50 to 750.
Answers
Answered by
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:
- START
- Iterate a loop within range 50 - 750.
- Check if any number is prime or not. If prime, add them up and store the sum in sum variable.
- Display the sum.
- STOP
Refer to the attachment for output ☑.
Attachments:
Similar questions