Thursday, October 20, 2016

Factorial in Ruby

In this simple program I called this program factorial in ruby what does the program will do is to ask the user to give a number and then our program will compute the factorial value of the given number by the user using Ruby as our programming language. I hope you like this program. Thank you.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com. 


My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com. 


My mobile number here in the Philippines is 09173084360.


Program Listing

factorial.rb


def fact(n)
 puts 1.upto(n).inject('*')
end

loop do
print "\n\n";
print "Factorial Program"
print "\n\n";
print "Give a Number : ";
val1 = gets.chomp
val1 = val1.to_i;
print "\n\n";
print "The factorial value of #{val1} is "
print fact(val1);
print "\n\n";
print "Do you want to continue? (y/n) : "
answer = gets.chomp.downcase
  if answer == "n"
print "\n\n";
print "End of Program";
print "\n\n";
break
  end
end




No comments:

Post a Comment