Sum of First n Prime Numbers in Java
In this tutorial, we will be heading towards writing the Java program to find the Sum of first n Prime numbers.
Before moving towards actually writing code first, let’s understand what is a prime number.
A Prime number is a natural number that is divisible only by itself and 1.
Following are prime numbers starting from 1:
2,3,5,7,11,13,17,19,23,29,……….
Java Program to find out the sum of First n Prime numbers :
import java.util.*; import java.io.*; class SumOfPrime { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); int n,sum=0,i=1,j; n=sc.nextInt(); int t=n; while(n!=0) { int count=0; for(j=1;j<=i;j++) { if(i%j==0) { count++; } } if(count==2) { sum=sum+i; n--; } i++; } System.out.println("Sum of first "+t+" prime numbers is "+sum); } }
Output: 5 Sum of first 5 prime numbers is 28
The first 5 prime numbers are 2,3,5,7,11.
Sum of first 5 prime number is 2+3+5+7+11 =28
The logic of the program :
For the above problem statement, we have to first find a prime number starting from 1, In-Line 14 to 20, we are finding a divisor of number starting from 1 (In this case we are checking whether i=1 is prime or not). Inline 21 we are checking if the number has only two divisors then it is a prime number. If we got any prime number then add this number in the value of a variable sum (In this case we add 1 value to sum value 0). Also, we reduce the value of n by 1 (Since we got the first prime number).
Description of each line of the above program :
- Line 1 and 2 denote the importing (including) of packages (folder).
- Line 3 denotes the creation of a class with keyword “class” and the class name is “SumOfPrime”.
- Line 4 denotes the starting of a class by using opening curly braces ” { “
- Line 5 denotes the main method. It includes the method signature of the main method.
- Line 6 denotes the starting of the main method by using opening curly braces ” { “.
- Line 7 denotes the scanner class which is an inbuilt class in java. It is used to take input from a user. By using the object of Scanner class which is (sc) we can take input from the user.
- Line 8 denotes the declaring of variables n, sum, i and j. And we also initialize some variables among them. (sum=0 and i=1).
- Line 9 denotes the taking of input from a user by using the nextInt() method.
- Line 10 denotes the creation of a temporary variable (t). In this program, we assign a value of n to a temporary variable(t).
Steps of Finding prime number and adding this prime number to the value of sum variable
- Line 11, In this we create the while loop. The condition (n!=0), denotes that control will not come out of this loop till (n!=0). When the condition present in while loop is true then control will come out of the loop.
- Line 12 denotes the starting of a while loop by using opening curly braces ” { “
- Line 13 denotes the declaration and initialization of variable count to zero. (count=0)
- Line 14 denotes the starting of for loop. In this for loop, we initialize variable j to 1 (j=1) and we check the condition that “Is (j<=i)” if a condition is true then control go inside the For loop.
- Line 15 denotes the starting of for loop by using opening curly braces ” { “
- Line 16 denotes the use of the IF statement. In this we check condition (i%j==0). If a condition is true then control will move inside the loop.
- Line 17 denotes the starting of an IF statement by using opening curly braces ” { “
- Line 18 denotes the increment of variable count by 1.
- Line 19 denotes the closing of if statement.
- Line 20 denotes the closing of for loop.
- Line 21 denotes using of IF statement to check condition (count==2). If a condition is true then all statements (lines) present in the IF statement are executed.
- Line 26 denotes the incrementing the value of the variable i by 1.
Step to print the value of sum variable
- Line 28 denotes the printing the “Sum of first n prime numbers ” by using System class.
- Line 29 and 30 denote the closing of the “main” method and “SumOfPrime” class.
Also read: How to call non static method from static method in Java
Leave a Reply