Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
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

Posted on December 18, 2020November 27, 2022

Table of Contents

  • Write a Java program to find the sum of the first n prime numbers
    • Code to calculate first n prime numbers
      • Code to calculate first n prime numbers using for loop
      • Code to calculate first n prime numbers using while loop
      • Code to calculate first n prime numbers using do-while loop
      • Code to calculate first n prime numbers using method

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes