Find prime numbers in the first half and second half of an array | C ++ program to print the prime numbers in the first half and second half of an array ; Function to check if a number is prime or not ; Function to find whether elements are prime or not ; Traverse in the given range ; Check if a number is prime or not ; Function to print the prime numbers in the first half and second half of an array ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool prime ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } void prime_range ( int start , int end , int * a ) { for ( int i = start ; i < end ; i ++ ) { if ( prime ( a [ i ] ) ) cout << a [ i ] << " ▁ " ; } } void Print ( int arr [ ] , int n ) { cout << " Prime ▁ numbers ▁ in ▁ the ▁ first ▁ half ▁ are ▁ " ; prime_range ( 0 , n / 2 , arr ) ; cout << endl ; cout << " Prime ▁ numbers ▁ in ▁ the ▁ second ▁ half ▁ are ▁ " ; prime_range ( n / 2 , n , arr ) ; cout << endl ; } int main ( ) { int arr [ ] = { 2 , 5 , 10 , 15 , 17 , 21 , 23 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; Print ( arr , n ) ; return 0 ; }