Java Program - Sum of Prime Numbers in a Range
Mathematics for Programming Numbers (Article) Numbers (Program)
21
Given Input:
Expected Output:
Program:
public class SumOfPrimes {
public static void main(String[] args) {
int start = 10, end = 50;
int sum = 0;
for (int i = start; i <= end; i++) {
if (isPrime(i))
sum += i;
}
System.out.println("Sum of prime numbers between " + start + " and " + end + " is: " + sum);
}
public static boolean isPrime(int number) {
if (number <= 1)
return false;
for (int i = 2; i <= number / 2; i++) {
if (number % i == 0)
return false;
}
return true;
}
}
Output:
This Particular section is dedicated to Programs only. If you want learn more about Mathematics for Programming. Then you can visit below links to get more depth on this subject.
Program:
public class SumOfPrimes { public static void main(String[] args) { int start = 10, end = 50; int sum = 0; for (int i = start; i <= end; i++) { if (isPrime(i)) sum += i; } System.out.println("Sum of prime numbers between " + start + " and " + end + " is: " + sum); } public static boolean isPrime(int number) { if (number <= 1) return false; for (int i = 2; i <= number / 2; i++) { if (number % i == 0) return false; } return true; } }
Output:
This Particular section is dedicated to Programs only. If you want learn more about Mathematics for Programming. Then you can visit below links to get more depth on this subject.