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
Java program to find sum of prime numbers from 1 to n

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

Posted on December 10, 2020December 11, 2020

Table of Contents

  • Java program to find sum of prime numbers
    • Code to calculate sum of prime numbers
      • Code to calculate sum of prime numbers using for loop
      • program to find sum of prime numbers using while loop
      • Program to find sum of prime numbers using do-while loop
    • Related

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

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