ruby def factorial(n) if n == 0 1 else n * factorial(n-1) end end puts “Enter a number to find its factorial:” number = gets.chomp.to_i result = factorial(number) puts “Factorial of #{number} is #{result}.”Step 1: define a method “factorial” with an integer n as its argument. Step 2: The method “factorial” checks if n is 0, then returns 1, because the factorial of 0 is 1. Otherwise, it recursively calls itself with n-1 as its argument, multiplying the result by n at each level until n reaches 0. Step 3: ask the user to input a number with the help of puts Step 4: convert the number to an integer using to_i and store it in a variable “number” Step 5: call the factorial method with the number as its argument, and output the result.
Write a program in ruby to find factorial of a number
I am sharing with you a program in Ruby to find the factorial of a number: