Q:

C++ Program To Check Primness of 1D Array

0

 Program To Check Elements of a 1-D Integer An Array For Primness. Means in array your task is to find the prime number in an array or C++ Program To Check Primness of an Array.


Logic:- To Check primness of array we need to know that how to find the prime number then use a loop and run the program that's it we solve the problem. So Prime number is a number which only can be divided by the 1 or number itself, here are some prime number 2, 3, 5, 7, 11, 13, 17, 19. Important notice here 1 is not a prime number. 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#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:

Enter The Size of Array

5

Enter The Element

3 6 7 9 15

3 is Prime

6 is Not Prime

7 is Prime

9 is Not Prime

15 is Not Prime

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now