how to find sum of first n prime numbers in java code example

Example: sum of prime numbers in a digit in java

import java.util.*;
public class PrimeDigit
{
    public boolean IsPrime(int i)
    {
        boolean b=true;
        int d=2;
        while(d<i/2)
        {
            if (i%d==0)
            {
                b=false;
                break;
            }
            d++;
        }
        return b;
    }
    public static void main(String []args)
    {
       Scanner sc=new Scanner(System.in);
       System.out.print("Enter A Number : ");
       int i=sc.nextInt();
       int s=0,r;
       PrimeDigit pd=new PrimeDigit();
       while(i>0)
       {
            r=i%10;
            if(pd.IsPrime(r))
			{
                s = s + r;
			}
            i = i / 10;
       }
       System.out.print("Sum Of The Prime Digits : " + s );
    }
}

Tags:

Java Example