Factorial of given number

Write Ruby program reads a number and calculates the factorial value of it and prints the same.
factorial.rb
print("Enter number:\n")
n = gets.to_i
fact=1
for i in 1..n
fact = fact*i
end
print("Factorial of #{n} is #{fact}.")
Output
Enter number:
4
Factorial of 4 is 24.

No comments:

Post a Comment

Total Pageviews