hai friends,

yesterday Our Thyagu Sir, took spl class for me and yogesh. I learnt very basics of ruby and “How to write a Good Programming Code?” The first day sessions goes abt how to find “simple interest of a amount?”, “print the sum of n numbers” and “a simple Multiplication table”

Find The Simple interest Of a Amount code:

# Program to calculate simple interest
=begin
program name:simple_rv.rb
Date written:18-12-2010
Date last modified:19-12-2010
Author:Tha.Suresh
License:LGPL
=end
# Main part of the program starts here
puts
print "Enter the principle: "
principle = gets.chomp.to_f
print "Number of years: "
number_of_years = gets.chomp.to_f
print "Rate of interest: "
rate_of_interest = gets.chomp.to_f
simple_interest = (principle * number_of_years * rate_of_interest)/100
puts
puts "The simple interest for the given data is: " + simple_interest.to_s
# Main part of the program ends here

This Program will give the interest amount.

Here, gets ‘ll print the data with “\n”

chomp ‘ll cut, that “\n” and print the data without “\n”

.to_f” ‘ll convert the string value into float. bcoz default Ruby takes the all value as “string value”

At last again we convert the integer value into “string” Bcoz when we can’t concordance “String + integer”

Ex:

[root@mercury suresh_class]# ruby simple_pnr.rb

Enter the principle: 10000

Number of years: 2

Rate of interest: 2

The simple interest for the given data is: 400.0

🙂

0.000000
0.000000

### Like this:

Like Loading...

*Related*