How to find the product of digits of a number by using for loop in PHP

1 Answer

0 votes
function get_product($n) {
    for ($product = 1; $n > 0; $n = (int)($n / 10)) {
        $reminder = $n % 10;
        $product = $product * $reminder;
    }
     
    return $product;
}
 
echo get_product(32) . "\n";
echo get_product(529) . "\n";



 
 
 
/*
run:
 
6
90
 
*/

 



answered Oct 8, 2021 by avibootz

Related questions

...