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
    • Related

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

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes