15.9.15

find prime numbers and make sum of all primes using c++

/* Find all primes and do the sum of those primes between a given range */

#include<iostream>
#include<conio.h>
using namespace std;
void prime_check(int first,int last)
{
    int c,j,count=0,sum=0;
    cout<<"The Number of Primes:"<<endl;
    for(int i=first;i<=last;i++)
    {
        c=1;
        for(j=2;j<=i/2;j++)
        {
            if(i%j==0)
            {
                c=0;
                break;
            }
        }
        if(c==1)
        {
            count++;
            sum+=i;
        }
    }
        cout<<count<<"\t"<<endl;;
        cout<<"The sum of the prime number is:"<<sum<<endl;
}
int main()
{
    int first,last;
    cout<<"Range Start from:"<<endl;
    cin>>first;
    cout<<"End of Range:"<<endl;
    cin>>last;
    prime_check(first,last);
    getch();
    return 0; }

No comments:

Post a Comment

Comment Here