Thursday, December 15, 2011

A Java program to print the sum of all prime numbers between 1 and 1000

/**
 * This Java program calculates and prints the sum of all prime numbers
 * between 1 and 1000.
 */

class prime1To1000
{
    public static void main()
    {
        int i,k,j;
        double sum=0;
        for(i=1;i<1000;i++)
        {
            k=0;
            for(j=2;j<i;j++)
            {
                if(i%j==0)
                {
                    k=1;
                    break;
                }
            }
            if(k==0)
            {
                sum=sum+i;
            }
        }
        System.out.println("Sum = "+sum);
    }
}

//Author : Mayank Rajoria

No comments:

Post a Comment