Table of Contents
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
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
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
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
Data type and variable in Java
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.