Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Tutorials Jruby Calculating factorial in JRuby

Related Tutorials


 
 

Share on Google+Share on Google+

Calculating factorial in JRuby

Advertisement
In the previous section of "Running first JRuby program" example you must be aware with the way to run JRuby program.

Calculating factorial in JRuby

     

In the previous section of "Running first JRuby program" example you must be aware with the way to run JRuby program. Now in this example you will know to "Calculate Factorial in JRuby".

In this example we have defined a function factorial( number) which takes the number as argument for which factorial is to be evaluated. Variable "n"  will consists of the evaluated result finally and we can print the result by calling function factorial(). Here is the example code for calculating factorial in JRuby as follows:

 

 

 


JRubyFactorail.rb

require "java"
# Define method factorial 
   def factorial(n)
  if n = = 0
   1
   else
  n * factorial(n-1)
   end
  end
# Printing factorial of 5 
puts factorial(5).to_i


 Output:

C:\JRuby>jruby JRubyFactorial.rb
120


Download Code

     

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: September 29, 2008

Related Tutorials

Discuss: Calculating factorial in JRuby  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com