Table of Contents
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
Data type and variable in Java
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