Program in C++ to find smallest prime number from array Using function
#include<iostream> using namespace std; int minPrime(int a[],int n) { int i,p,j,s; 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]<s) s=a[i]; } } return s; } 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<<"Smallest Prime numbers "<<minPrime(arr,n); return 0; }
Output:-
Note: If not any prime number present in array.
#include<iostream> using namespace std; int minPrime(int a[],int n) { int i,p,j,s,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]<s) s=a[i]; } } if(flag==0) { cout<<"Not any elements are prime no.!"; return 0; } return s; } int main() { const int size=35; int arr[size],n,m; 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]; } m=minPrime(arr,n); if(m>0) cout<<"Smallest Prime numbers "<<m; return 0; }
Output:-
Related programs
0 Comments