Ruby Programms of all types :: Most Important :: Run Each and Every Program individually
https://www.includehelp.com/ruby/programs.aspx
Ruby ::
# The Greeter classclassGreeterdefinitialize(name) @name=name.capitalizeenddefsaluteputs "Hello #{@name}!"endend# Create a new objectg=Greeter.new("world")# Output "Hello World!"g.salute
Ruby programs/examples
Ruby basic programs
Ruby program to check whether the given number is prime or not
Ruby program to find number of characters in a string (string length)
Ruby program to check whether the hash contains specified key or not
Ruby program to convert the string into lowercase and uppercase
Ruby program to check whether a string contains a substring or not
Ruby program to check whether an element exists in an array or not
Ruby program to calculate the sum of all odd numbers up to N
Print the elements of a set in Ruby
How to check if a hash key exists in Ruby
How to add elements to a Hash in Ruby?
TOP Interview Coding Problems/Challenges
Run-length encoding (find/print frequency of letters in a string)
Checking Anagrams (check whether two string is anagrams or not)
Check whether a Binary Tree is BST (Binary Search Tree) or not
Ruby code to print "Hello World!"
=begin Ruby program to print Hello World. =end puts "Hello World!" print "Hello World!" puts "Hello World!"
Output
Hello World! Hello World!Hello World!
Adding Two Numbers in Ruby::
Methods Used ::
puts :: This method is used to display some message to the user
gets :: This method is used to take input from the user
.to_i :: When we take input through gets method it is a string.for further calculations it is mandatory to convert it into an integer for exact results and this method returns the integer conversion of the given string
Varibale Used ::
num1 :: to store first value
num2 :: to store second value
sum :: to store the sum i.e. result
Progrm ::
=begin
Ruby program to add two numbers
=end
puts “Enter First Value: ”
num1=gets.chomp.to_i
puts “Enter Second Value:”
num2=gets.chomp.to_i
sum=num1+num2
puts “The sum is #{sum}”
Program ::
=begin
Ruby program to find the area of the Rectangle
=end
puts “Enter Length:”
l=gets.chomp.to_f
puts “Enter width:”
w=gets.chomp.to_f
area=l*w
puts “Area of Rectangle is #{area}”
Comments
Post a Comment