Program in C++ to find greatest prime number from array Using function
#include<iostream> using namespace std; int maxPrime(int a[],int n) { int i,p,j,l=0; for(i=0;i<n;i++) { for(p=1,j=2;j<=a[i]/2;j++) { if(a[i]%j==0) { p=0; break; } } if(p==1) { if(a[i]>l) l=a[i]; } } return l; } int main() { const int size=35; int arr[size],n,m=0; cout<<"Enter the size of array:-"; cin>>n; if(n>size) { cout<<"Invalid array size!."; return 0; } cout<<"Enter "<<n<<" numbers:-"; for(int i=0;i<n;i++) { cin>>arr[i]; } cout<<"Greatest Prime numbers "<<maxPrime(arr,n); return 0; }
Output:-
Note: If not any prime number present in array.
#include<iostream> using namespace std; int maxPrime(int a[],int n) { int i,p,j,l=0,flag=0; for(i=0;i<n;i++) { for(p=1,j=2;j<=a[i]/2;j++) { if(a[i]%j==0) { p=0; break; } } if(p==1) { flag=1; if(a[i]>l) l=a[i]; } } if(flag==0) { cout<<"Not any elements are prime no.!"; return 0; } return l; } int main() { const int size=35; int arr[size],n,l; cout<<"Enter the size of array:-"; cin>>n; if(n>size) { cout<<"Invalid array size!."; return 0; } cout<<"Enter "<<n<<" numbers:-"; for(int i=0;i<n;i++) { cin>>arr[i]; } l=maxPrime(arr,n); if(l>0) cout<<"Greatest Prime numbers "<<l; return 0; }
Output:-
0 Comments