Answers for "Given n, find the factorial and print the trailing zeros"

0

number of trailing zeros in a factorial of a number.

// Number of trailing zeros in a factorial of a number

fn zeros(mut n: u64) -> u64 {
    let mut zeros = 0;
    while n > 0 {
        n /= 5;
        zeros += n;
    }
    zeros
}

fn main() {
    println!("Number of trailing zeros = {} ", zeros(14));  // 2
    println!("Number of trailing zeros = {} ", zeros(30));  // 7
}
Posted by: Guest on September-12-2021
0

number of trailing zeros in a factorial of a number.

# Number of trailing zeros in a factorial of a number

def zeros(n)
  zeros = 0
  while (n > 0) 
    n = (n / 5).floor
    zeros += n
  end
  zeros  
end

print "Number of trailing zeros = ", zeros(6);   # 1
print "\nNumber of trailing zeros = ", zeros(30);  # 7
Posted by: Guest on September-12-2021

Code answers related to "Given n, find the factorial and print the trailing zeros"

Python Answers by Framework

Browse Popular Code Answers by Language