Modify a given array by replacing each element with the sum or product of their digits based on a given condition | C ++ program for the above approach ; Function to modify the given array as per the given conditions ; Traverse the given array arr [ ] ; Initialize the count of even and odd digits ; Initialize temp with the current array element ; For count the number of even digits ; Increment the odd count ; Otherwise ; Divide temp by 10 ; Performe addition ; Performe multiplication ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void evenOdd ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int even_digits = 0 ; int odd_digits = 0 ; int temp = arr [ i ] ; while ( temp ) { if ( ( temp % 10 ) & 1 ) odd_digits ++ ; else even_digits ++ ; temp /= 10 ; } if ( even_digits > odd_digits ) { int res = 0 ; while ( arr [ i ] ) { res += arr [ i ] % 10 ; arr [ i ] /= 10 ; } cout << res << " ▁ " ; } else if ( odd_digits > even_digits ) { int res = 1 ; while ( arr [ i ] ) { res *= arr [ i ] % 10 ; arr [ i ] /= 10 ; } cout << res << " ▁ " ; } else cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 113 , 141 , 214 , 3186 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; evenOdd ( arr , N ) ; return 0 ; }