Ruby language
A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.
Here we are going to start with basic programs which will be useful for beginners.
10 Basic Ruby programs
*********************************************************************
- Find the area of the Rectangle
puts “Enter the length:”
l = gets.chomp.to_f
puts “Enter the Width:”
w = gets.chomp.to_f
#calculating the area
area = l * w
#Printing the area
puts “Area of rectangle is:#{area}”
Output:
F:\ruby\ruby2> ruby .\area_of_rectange.rb
Enter the lengith:
3
Enter the Width:
4
Area of rectange is:12.0
PS F:\ruby\ruby2>
********************************************************************
2. Write a Program to find a year is leap year or not
# To find the leap year or not
puts “Enter the year which you want to check:”
year = gets.chomp.to_i
if year % 400 == 0
puts “#{year} is a leap yeaar”
elsif year % 4 == 0 && year % 100 !=0
puts “#{year} is a leap year\m”
else
puts “#{year} is not leap year”
end
OUTPUT:
F:\ruby\ruby2> ruby .\leap_year.rb
Enter the year which you want to check:
2000
2000 is a leap yeaar
PS F:\ruby\ruby2> ruby .\leap_year.rb
Enter the year which you want to check:
2022
2022 is not leap year
******************************************************************
3. Write a ruby program to find leap year in given range
# Find a leap year between ranges
puts “Enter the starting year:”
lower = gets.chomp.to_i
puts “Enter the ending year:”
upper = gets.chomp.to_i
for year in lower..upper do
if year % 400 == 0
puts “#{year} is a leap year”
elsif year % 4 == 0 && year % 100 !=0
puts “#{year} is a leap year”
else
puts “#{year} is not a leap year”
end
end
OUTPUT:
ruby .\Leap_year_ranges.rb
Enter the starting year:
2000
Enter the ending year:
2010
2000 is a leap year
2001 is not a leap year
2002 is not a leap year
2003 is not a leap year
2004 is a leap year
2005 is not a leap year
2006 is not a leap year
2007 is not a leap year
2008 is a leap year
2009 is not a leap year
2010 is not a leap year
*****************************************************************
4.Write a ruby program to calculate the power of a number
# ruby program that prints power of a number
def pow(a,b)
power = 1
for i in 1..b
power = power * b
end
return power
end
puts “Enter the base number:”
base = gets.chomp.to_i
puts “Enter the Expo number:”
expo = gets.chomp.to_i
puts “The power is #{pow(base,expo)}”
OUTPUT:
PS F:\ruby\ruby2> ruby .\power_of_number.rb
Enter the base number:
4
Enter the Expo number:
4
The power is 256
PS F:\ruby\ruby2>
**************************************************************
5. Write a ruby program to print fibonacci series
# Program to find Fibonacci series
def fib(n)
if (n<=2)
return 1
else
return (fib(n-1) + fib(n-2))
end
end
puts “Enter the number of items:”
n =gets.chomp.to_i
Get Ranjithkumar Khan’s stories in your inbox
Join Medium for free to get updates from this writer.
puts “The fibonacci series of this #{n} are”
for c in 1..n
puts fib(c)
end
OUTPUT:
Enter the number of items:
8
The fibonacci series of this 8 are
1
1
2
3
5
8
13
21
**************************************************************
6. Write a ruby program to find a prime number
# Program to find Prime number
puts “Enter the number:”
a = gets.chomp.to_i
count = 0
if ( a == 0)
puts “0 is a not prime number”
else
i = 2
while (i < a)
if (a %i ==0)
count +=1
end
i+=1
end
end
if count > 1
puts “#{a} is not a prime number”
else
puts “#{a} is a prime number”
end
OUTPUT:
F:\ruby\Ruby2> ruby .\prime_number.rb
Enter the number:
10
10 is not a prime number
PS F:\ruby\Ruby2> ruby .\prime_number.rb
Enter the number:
2
2 is a prime number
**************************************************************
7.Write a ruby program to find the factorial of a given number
# Program to find the factorial of a given number
puts (“Enter the number:”)
num = gets.chomp.to_i
fact = 1
if (num==0)
puts (“Invalid number, cann’t find the factorial”)
else
i = 1
while(i<=num)
fact = fact * i
i+=1
end
end
puts (“The factorial of a #{num} is #{fact}”)
OUTPUT:
F:\ruby\Ruby2> ruby .\factorial.rb
Enter the number:
6
The factorial of a 6 is 720
PS F:\ruby\Ruby2> ruby .\factorial.rb
Enter the number:
0
Invalid number, can’t find the factorial
**************************************************************
8.Write a ruby program to check whether a variable is defined or not
var1 = 10
var2 = 40
var3 = 50
str1 = ‘Mac OS’
str2 = ‘Windows’
puts local_variables.include?(:var1)
puts local_variables.include?(:var2)
puts local_variables.include?(:var3)
puts local_variables.include?(:var4)
puts local_variables.include?(:str4)
puts local_variables.include?(:str1)
OUTPUT:
PS F:\ruby\Ruby2> ruby .\defined_variable.rb
true
true
true
false
false
true
**************************************************************
9.Write a ruby Progam to add the even numbers
# Progam to add the even numbers
puts “Enter the number:”
num = gets.chomp.to_i
sum = 0
i = 1
while (i<=num)
if (i%2 == 0)
sum = sum +i
i=i+1
else
i=i+1
end
end
puts “The sum is #{sum}”
OUTPUT:
ruby .\sum_of_even_number.rb
Enter the number:
4
The sum is 6
*************************************************************
10. Write a program for multiplication
# Program for multiplication
puts “Enter the number:”
num = gets.chomp.to_i
for i in 1..10
multiply = num * i
puts “#{num} * #{i} = #{multiply}”
end
OUTPUT:
F:\ruby\Ruby2> ruby .\multiplicaton.rb
Enter the number:
5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50







