15.9.15

find prime and check with the sum of all primes using c++

/* Print Prime List of the given range and do the sum then check weather the number is equal to the given number */

#include<iostream>
#include<conio.h>
using namespace std;
int prime_equal(int n)
{
    int c,sum=0;
    for(int i=1;i<=n;i++)
    {
        c=1;
        for(int j=2;j<=i/2;j++)
        {
            if(i%j==0)
            {
                c=0;
                break;
            }
        }
        if(c==1)
        {
            cout<<i<<"\t";
            sum+=i;
        }
    }
    cout<<endl;
    cout<<"The sum is:"<<sum<<endl;
    if(sum==n)
    {
        cout<<"The sum is equal to "<<n<<endl;
    }
    else
    {
        cout<<"The sum is not equal to "<<n<<endl;
    }
 return 0;
}
int main()
{
    int n;
    cout<<"Enter Range:"<<endl;
    cin>>n;
    cout<<"prime List:"<<endl;
    prime_equal(n);
    return 0;
}

No comments:

Post a Comment

Comment Here