Sum of prime numbers in Java
In this post, you will learn how to find the sum of prime numbers in Java.
A prime number is a whole number greater than 1, whose only factors are 1 and itself, like -2, 3, 5, 7, 11 etc. For example, 17 is a prime number because it is only divisible by 1 and 17. On the other hand, 18 is not a prime number because it is divisible by 2, 3, 6, 9 and the number itself.
There are different ways to find the sum of prime numbers in Java. Here, we have mentioned most of them.
Sum of prime numbers in Java using for loop
In the given example, we have used the for loop to calculate the sum of prime numbers between 1 and 100 in Java.
public class SumOfPrimeNumbers
{
public static void main(String args[])
{
int counter, sum = 0;
for(int num = 1; num <= 100; num++)
{
counter = 0;
for (int i = 2; i <= num/2; i++)
{
// check for prime number
if(num % i == 0)
{
counter++;
break;
}
}
// calculates the sum of prime numbers
if(counter == 0 && num != 1 )
{
sum = sum + num;
}
}
System.out.println("The sum of prime numbers from 1 to 100 : " + sum);
}
}
Output of the above code:
The sum of prime numbers from 1 to 100 : 1060
Sum of prime numbers in Java using while loop
The given Java program calculates the sum of prime numbers between 1 and 150 using the while loop.
public class SumOfPrimeNumbers
{
public static void main(String args[])
{
int sum = 0, num = 1;
while(num <= 150)
{
int i = 2, counter = 0;
while(i <= num/2 )
{
if(num % i == 0)
{
counter++;
break;
}
//increments the variable i by 1
i++;
}
// calculates the sum of prime numbers
if(counter == 0 && num != 1 )
{
sum = sum + num;
}
num++;
}
//prints the sum
System.out.println("The Sum of Prime Numbers from 1 to 150 : " + sum);
}
}
Output of the above code:
The Sum of Prime Numbers from 1 to 150 : 2276
Sum of prime numbers in Java using user-defined function
In the given Java program, we have defined a function addPrimeNumbers() to calculate the sum of prime numbers between the given min and max range.
public class SumOfPrimeNumbers
{
//function that checks if the given number is prime or not
static boolean checkPrime(int num)
{
if(num == 1)
{
return false;
}
for (int i = 2; i*i <= num; i++)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
// method to iterate over the min and max,
// detect the prime number
// and calculate the sum
static int addPrimeNumbers(int min, int max)
{
int sum = 0;
for (int i = max; i >= min; i--)
{
boolean prime = checkPrime(i);
if (prime)
{
sum = sum + i;
}
}
return sum;
}
//driver code
public static void main(String args[])
{
int min = 0, max = 70;
//call the user-defined method
System.out.println("The sum of Prime Numbers between the given range : " +addPrimeNumbers(min, max));
}
}
Output of the above code:
The sum of Prime Numbers between the given range : 568
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java