calculations

Java program to find sum of prime numbers from 1 to n

Java program to find sum of prime numbers

In this article, we will discuss the concept of Java program to find sum of prime numbers from 1 to n

In this program, we are going to learn how to calculate sum of prime numbers 1 to n using different methods in Java language.

This is done using for loop,while loop,do-while loop in Java language

Code to calculate sum of prime numbers

Code to calculate sum of prime numbers using for loop

In this program, we will calculate sum of prime numbers 1 to n using for loop in Java language

Program 1

//find sum of prime numbers from 1 to n
import java.util.*;
public class PrimeSum{
public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  //scanner class for input
  
int i,num,count, sum=0;
System.out.println("Please Enter maximum value: ");
int maxValue=sc.nextInt();

System.out.println("Sum of Prime numbers between 1 to "+maxValue+" are : ");

//forloop for finding and printing all prime numbers in given range
for(num=1; num<=maxValue; num++){
  count=0;
  for(i=2; i<=num/2; i++){
    if(num%i==0){
      count++;
      break;
    }
}
if(count==0 && num !=1){
  sum+=num;
}
}
  System.out.println(sum);
}
}

When the above code is executed, it produces the following result

Please Enter maximum value
50
Sum of Prime numbers between 1 to 50 are :
328

 

program to find sum of prime numbers using while loop

In this program, we will calculate sum of prime numbers 1 to n using while loop in Java language

Program 2

//find sum of prime numbers from 1 to n
import java.util.*;
public class PrimeSum_While{
public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  //scanner class for input
  
int i,num,count, sum=0;
System.out.println("Please Enter maximum value: ");
int maxValue=sc.nextInt();

System.out.println("Sum of Prime numbers between 1 to "+maxValue+" are : ");

//forloop for finding and printing all prime numbers in 1 to new
num=1;
while( num<=maxValue){
  count=0;
  i=2; 
  while(i<=num/2){
    if(num%i==0){
      count++;
      break;
    }
     i++;
}
if(count==0 && num !=1){
  sum+=num;
}
num++;
}
  System.out.println(sum);
}
}

When the above code is executed, it produces the following result

Please Enter maximum value
25
Sum of Prime numbers between 1 to 25 are :
100

 

Program to find sum of prime numbers using do-while loop

In this program, we will calculate sum of prime numbers 1 to n using Do-while loop in Java language

Program 3

//find sum of prime numbers from 1 to n
import java.util.*;
public class PrimeSum_DoWhile{
public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  //scanner class for input
  
int i,num,count, sum=0;
System.out.println("Please Enter maximum value: ");
int maxValue=sc.nextInt();

System.out.println("Sum of Prime numbers between 1 to "+maxValue+" are : ");

//forloop for finding and printing all prime numbers in 1 to new
num=1;
do{
  count=0;
  i=2; 
do{
    if(num%i==0){
      count++;
      break;
    }
     i++;
} while(i<=num/2);
if(count==0 && num !=1){
  sum+=num;
}
num++;
}while( num<=maxValue);
  System.out.println(sum);
}
}

 

When the above code is executed, it produces the following result

Please Enter maximum value
200
Sum of Prime numbers between 1 to 200 are :
4227

 

Suggested for you

if statements in Java language

Nested if in Java language

Data type and variable in Java

Operator in Java language

Method in Java language

For loop in Java language

while loop in Java language

Do-while loop in Java language

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.