Count number of trailing zeros in product of array | CPP program for count total zero in product of array ; Returns count of zeros in product of array ; count number of 2 s in each element ; count number of 5 s in each element ; return the minimum ; Driven Program
#include <iostream> NEW_LINE using namespace std ; int countZeros ( int a [ ] , int n ) { int count2 = 0 , count5 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( a [ i ] % 2 == 0 ) { a [ i ] = a [ i ] / 2 ; count2 ++ ; } while ( a [ i ] % 5 == 0 ) { a [ i ] = a [ i ] / 5 ; count5 ++ ; } } return ( count2 < count5 ) ? count2 : count5 ; } int main ( ) { int a [ ] = { 10 , 100 , 20 , 30 , 50 , 90 , 12 , 80 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << countZeros ( a , n ) ; return 0 ; }