# C++ Program To Check Primness of 1D Array

Problem:- 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.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.

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. Check Here C++ Program To Find Number Is Prime Or Not.

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Solution:-

#include<iostream>
using namespace std;
int main()
{
int a,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:-

Previous Post
Next Post

#### post written by: Ghanendra Yadav

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.