Sunday, March 17, 2013

Searching Prime Numbers In A Given Vector

#include<iostream>
using namespace std;
int checkprime(int);
int main()
{
    int arr[50];
    int n;
    cout<<"Enter the size:";
    cin>>n;
    cout<<"Enter the array elements: \n\n";
    for(int i=0;i<n;++i)
    {
        cout<<"Enter "<<i+1<<" Element: ";
        cin>>arr[i];
    }
    cout<<"\n\n";
    for(int i=0;i<n;++i)
    {
        cout<<"The "<<i+1<<" Term is: "<<arr[i]<<" "<<"\n";
    }
    for(int j=0;j<n;++j)
    {
        if(checkprime(arr[j])==1)
        cout<<arr[j]<<" "<<"-"<<" "<<"It is prime!!!!!\n";
        else
        cout<<arr[j]<<" "<<"-"<<" "<<"It is composite!!!!!\n";
    }
    return 0;
}
int checkprime(int a)
{
    int flag=1;
    for(int i=2;i<=a/2;++i)
    {
        if(a%i==0)
        {
            flag=0;
            break;
        }
    }
    return flag;
}

0 comments:

Post a Comment