Wednesday, 10 July 2013

Sum of Prime Numbers from 1 to 1000 in C++

#include<iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int a,b,temp;
    double sum = 0;
   
    cout<<"--------------------------------------\n";
    cout<<"  Sum of Prime Numbers from 1 to 1000  \n";
    cout<<"--------------------------------------\n\n";
   
    for(a = 2 ; a <= 1000 ; a++)
    {
        temp=0;
        for(b = 2 ; b < a ; b++)
        {
            if(a % b == 0)   
            temp = 1;   
        }
        if(temp == 0)
        sum = sum + a ;
    }
    cout<<sum;
    getch();
}

No comments:

Post a Comment