java

Write a Java program to find the sum of the first n prime numbers

Write a Java program to find the sum of the first n prime numbers

In this article, we will discuss the concept of Write a Java program to find the sum of the first n prime numbers

In this code, we are going to learn how to write to calculate sum of the first n prime numbers  using different methods in Java program.

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

Code to calculate first n prime numbers

Code to calculate first n prime numbers using for loop

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

Program 1

import java.util.*;
public class PrimeSum_firstN{
public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  //scanner class for input
int i=1,sum=0;
System.out.println("Please Enter value for n: ");
int n=sc.nextInt();
int temp=n;
while(n!=0)
{
  int count=0;
  for(int j=1; j<=i; j++){
    if(i%j==0)
    {
      count++;
    }
  }
  if(count==2){
    sum=sum+i;
    n--;
  }
  i++;
}
  System.out.println("Sum of first "+temp+" Prime numbers are : "+sum);
}

}

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

Please Enter value for n:
25
Sum of first 25 Prime numbers are :1060

 

Code to calculate first n prime numbers using while loop

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

Program 2

import java.util.*;
public class PrimeSum_firstNWhile{
public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  //scanner class for input
int i=1,sum=0;
System.out.println("Please Enter value for n: ");
int n=sc.nextInt();
int temp=n;
while(n!=0)
{
  int count=0;
  int j=1;
  while(j<=i){
    if(i%j==0)
    {
      count++;
    }
     j++;
  }
  if(count==2){
    sum=sum+i;
    n--;
  }
  i++;
}
  System.out.println("Sum of first "+temp+" Prime numbers are : "+sum);
}

}

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

Please Enter value for n:
10
Sum of first 10 Prime numbers are :129

 

Code to calculate first n prime numbers using do-while loop

In this program, we will calculate sum of the  first n  prime numbers using do-while loop in Java language

Program 3

import java.util.*;
public class PrimeSum_firstNDoWhile{
public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  //scanner class for input
int i=1,sum=0;
System.out.println("Please Enter value for n: ");
int n=sc.nextInt();
int temp=n;
while(n!=0)
{
  int count=0;
  int j=1;
  do{
    if(i%j==0)
    {
      count++;
    }
     j++;
  }while(j<=i);
  if(count==2){
    sum=sum+i;
    n--;
  }
  i++;
}
  System.out.println("Sum of first "+temp+" Prime numbers are : "+sum);
}

}

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

Please Enter value for n:
50
Sum of first 10 Prime numbers are :5117


Code to calculate first n prime numbers using method

In this program, we will calculate sum of the  first n  prime numbers using method in Java language

Program 4

//find sum of prime numbers from 1 to n using method
import java.util.*;
public class PrimeSum_Method{
public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  //scanner class for input
int num=2,count=0, 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 : ");
while(count<maxValue){
  if(isprimeNumber(num)){
    sum+=num;
    count++;
  }
  num++;
  }
  System.out.println(sum);
}
private static boolean isprimeNumber(int num){
  for(int i=2; i<=num/2; i++){
    if(num%i==0){
      return false;
  }
}
return true;
}
}

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

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

 

Suggested for you

if statements in Java language

Nested if in Java language

Data type and variable in Java

Operator in Java language

The method in Java language

For loop in Java language

while loop in the 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.