gulzaar

Sum of n Prime Number

Feb 21st, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.70 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class que4_prime {
  5.  
  6.     public static void main(String[] args) {
  7.         // TODO Auto-generated method stub
  8.        
  9.        
  10.          int r, sum=0;
  11.             System.out.print("Enter Range:");
  12.             Scanner get = new Scanner(System.in);
  13.             r = get.nextInt();
  14.             System.out.println();
  15.             for(int i=2;i<= r;i++)
  16.             {
  17.                 int flag=0;
  18.                 for(int j=2;j<i;j++)
  19.                 {
  20.                     if(i%j==0)
  21.                         flag=1;
  22.                 }
  23.                 if(flag==0)
  24.                 {
  25.                     System.out.print(i+" ");
  26.                     sum=sum+i;
  27.                 }
  28.             }
  29.             System.out.println("\nSum = "+sum);
  30.        
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment