Skip to content

Getting started with ruby

13/12/2010

This day i started to learn ruby.

started with find volume_of_the_cylinder

print “Enter the value of radius:”
radius=gets.chomp.to_f               #convert to float value
print “Enter the value of height:”
height=gets.chomp.to_f
volume=(3.14*radius*radius*height)
print “volume of the cylinder:”,volume,”\n”

shan@shan:~/Documents/pro/Day1$ ruby volume_of_the_cylinder.rb
Enter the value of radius:4.5
Enter the value of height:10
volume of the cylinder:635.85

during this program i learned the difference between print and puts

puts print new line

Ex:

irb(main):001:0> 5.times {
irb(main):002:1* puts “dd”
irb(main):003:1> sleep 2
irb(main):004:1> }
dd
dd
dd
dd
dd
=> 5

print does not print a new line

Ex:

irb(main):009:0> 5.times {
irb(main):010:1* print “dd”
irb(main):011:1> sleep 2
irb(main):012:1> }
dddddddddd=> 5

Find greatest,greater,smallest from given three numbers.

# finding the greatest greater and smallest number
print “Enter the three values:”
a=gets.chomp.to_i
b=gets.chomp.to_i
c=gets.chomp.to_i
#it will return the greatest no from bc
d_gr=(b+c+(b-c).abs)/2
#it will return the smallest no from bc
d_sm=(b+c-(b-c).abs)/2
#it will return greatest no from d_gr a
greatest=(a+d_gr+(a-d_gr).abs)/2
#it will return smallest no from d_sm a
smallest=(a+d_sm-(a-d_sm).abs)/2
#find the greater number based on smallest and greatest
if ((greatest == a and smallest == b) or (greatest == b and smallest == a))
greater=c
end
if ((greatest == b and smallest == c) or (greatest == c and smallest == b))
greater=a
end
if ((greatest == c and smallest == a) or (greatest == a and smallest == c))
greater=b
end
print “Greatest:”+greatest.to_s,”\n”
print “Greater:”+greater.to_s,”\n”
print “Smallest:”+smallest.to_s,”\n”

in this program easily find big no using d_gr=(b+c+(b-c).abs)/2

add addition and difference of two no and divide 2 it gives big value

abs ==> is a function to produce absolute value.

Advertisements

From → Ruby

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: