Sunday, 1 November 2015

C++ Program To Check Primness Of An 1-D Array

Problem :- Program To Check Element's of a 1-D Integer An Array For Primness .means in array your task is to find prime number in array 

Logic :- To Check primness of array we need to know that how to find prime number then use extra loop and run the program that't it you solve the problem Check Here C++ Program To Find Number Is Prime Or Not .

Solution :-

#include<iostream>
using namespace std;
int main()
{
  int a[100],i,n,k,j,flag;
 
cout<<"Enter The Size of Array\n";
  cin>>n;

  cout<<"Enter The Element\n";
  for(i=0;i<n;i++)
  {
  cin>>a[i];
  }
cout<<"\n\n";
  for(i=0;i<n;i++)
  {
flag=0;
  k=a[i]/2;
  for(j=2;j<=k;j++)
  {
    if(a[i]%j==0)
    {
    flag=1;
break;  
}
   
  }
  if(flag==1)
  cout<<a[i]<<" is Not Prime\n";
  else
  cout<<a[i]<<" is Prime\n";
  }
return 0;

}

Output:-

C++ Program To Check Primeness Of An 1-D Array

No comments:
Write comments

Recommended Posts × +