In Java, to find sum of all prime numbers in a given range we can use loop concepts such as while loop or for loop. If the two input numbers are 1 and 5, then the output will be 10 (2+3+5=10).
In this article, we are focusing on learning through examples, logic and program explanation for better understanding
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
Problem Statement
We have to write a Java program to print sum of all Prime numbers in a given range. The program should take two input values as range and should print the sum of prime numbers in that range. Let’s see an example for better understanding
Example:
For the input range 1 to 6, output should be 10
Explanation:
Input first number: 1
Input second number: 6
Prime numbers between 1 and 6: 2 3 5
Sum of prime numbers: 10
Hence, 10 is the sum of prime numbers between 1 and 6.
Logic to find sum of all prime numbers in a given range
- Take two values firstNum and lastNum as a range.
- Initialize a variable sum to 0 o store the accumulated sum of prime numbers.
- Use a loop (while or for loop) that iterates from firstNum to lastNum (inclusive).
- Inside the loop, initialize a boolean variable isPrime to true, assuming the current number might be prime.
- Using if statement, check if firstNum is 1. If true, sets isPrime to false because 1 is not considered a prime number.
- It uses inner loop (while or for loop) with variable i starting from 2 up to firstNum-1.
- Inside this loop, using if statement it checks if firstNum% i == 0. If firstNum divisible by i, sets isprime to false to mark it as non-prime and breaks out of the inner loop using break.
- After the inner loop, using if statement, if isPrime remains true, it means the current number is prime. Adds firstNum to sum.
- After completing the loop through the range, prints the accumulated sum of prime numbers (sum).
Java program to find sum of all prime numbers in a given range
Program 1: Using while loop
In the below program we are using while loop to print sum of all prime numbers in a given range.
import java.util.Scanner;
public class PrimeNumbersSum {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter first number");
int firstNum = s.nextInt();
System.out.println("Enter second number");
int lastNum = s.nextInt();
int sum = 0;
while (firstNum <= lastNum) {
boolean isPrime = true;
if (firstNum != 1) {
int i = 2;
while (i < firstNum) {
if (firstNum % i == 0) {
isPrime = false;
break;
}
i++;
}
}
else
isPrime = false;
if (isPrime)
sum+=firstNum;
firstNum++;
}
System.out.println("Sum of Prime Numbers are= " + sum);
s.close();
}
}
Output:
Enter first number
1
Enter second number
9
17
Java program explanation to print sum of all prime numbers in a given range
- The Java program uses a
Scanner
to read two integers,firstNum
andlastNum
. - Initializes
sum
to accumulate the sum of prime numbers betweenfirstNum
andlastNum
. - Enters a
while
loop iterating fromfirstNum
tolastNum
. - Checks if each number is prime by testing divisibility from
2
up to the number itself. - If a number is prime, adds it to
sum
. - Outputs the total sum of prime numbers found within the specified range.
- Closes the
Scanner
instance after processing input.
Program 2: Using for loop
In the below program we are using for loop to print sum of all prime numbers in a given range.
import java.util.Scanner;
public class PrimeNumbersSum {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter first number");
int firstNum = s.nextInt();
System.out.println("Enter second number");
int lastNum = s.nextInt();
int sum = 0;
for (int number= firstNum; number <= lastNum; number++) {
boolean isPrime = true;
if (number != 1) {
for (int i = 2;i < number; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
}
else
isPrime = false;
if (isPrime)
sum+=number;
}
System.out.println("Sum of Prime Numbers are= " +sum);
s.close();
}
}
Output:
Enter first number
55
Enter second number
100
679
Java program explanation to print sum of all prime numbers in a given range
- The program uses a
Scanner
to read two integers,firstNum
andlastNum
. - Initializes
sum
to accumulate the sum of prime numbers betweenfirstNum
andlastNum
. - Uses a
for
loop to iterate through each number fromfirstNum
tolastNum
. - Checks if each number is prime by testing divisibility from
2
up to the number itself. - If a number is prime, adds it to
sum
. - Outputs the total sum of prime numbers found within the specified range.
- Closes the
Scanner
instance after processing input.
Conclusion
In this article, we have learnt how to find sum of all prime numbers in a given range in Java. We understood the logic and then moved on to program implementation. Hope this article helped you in understanding the program efficiently.
You can also check our another awesome tutorials
- Java program to print all prime numbers in a range
- Program to find all prime factors in a range in Java
- Check perfect number in Java
- Java program to print weekday for a given input week
Happy Programming!