01/11/2015

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.

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post

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


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


You May Also See


1. C++ Program To Find Number Is Even / Odd Using Pointer

2. C++ Program To Read Infinite Number Then Arrange Ascending Order Using Pointer

3. C++ Program To Average Of Array In Function Of Using Pointer

4 C++ Program For Swapping Two Number In Function Using Pointer



Previous Post
Next Post

post written by:

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.
Follow Me

0 Comments: