Skip to content

Instantly share code, notes, and snippets.

View chiragmongia's full-sized avatar

Chirag Mongia chiragmongia

View GitHub Profile
class Vehicle
def getdata
p "Enter the name of the vehicle"
@name = gets.chomp
p "Enter the price of the vehicle"
@price = gets.chomp
end
def showdata
p "Name of vehicle: #{@name}"
def factorial(num)
p (1..num).inject(:*) || 1
end
p "Factorial Using Ranges"
print "Enter the number: "
num = gets.to_i
print "Factorial is: "
factorial(num)
def reverse(string)
arr = string.split
arr.reverse!
p arr.join(" ")
end
p "Enter the string"
string = gets.chomp
reverse(string)
p "Enter the string"
line = gets.chomp
arr = line.split(//)
lower = 0
upper = 0
num = 0
other = -1
for i in 0..arr.size
case arr[i]
p "Enter the string"
line = gets.chomp
p "Enter pattern to be searched"
pattern = gets.chomp
p matchfound = line.gsub(/#{pattern}/,"(#{pattern})")
print "Number of occurences- "
p line.scan(/#{pattern}/).size
class Array
def power(x)
@x = x
@f= []
self.each do |i|
b = i
c = 1
@x.times{ c = c * b }
@f << c
end
def prime(n)
prime_no = []
a =0
if n==2
print "#{n} is prime no"
end
prime_no << 2
3.step(n,1) do |num|
2.step(num/2,1) do |k|
if num % k == 0
def fact(n)
if n == 0
1
else
n * fact(n-1)
end
end
def pascal_row(n)
for k in 0..n
class String
def to_s
puts "---Swapping the case of string---"
p self.swapcase
end
end
p "Enter the string"
line = gets.chomp
line.to_s
#Question- Fibonacci - Yield
#Fibonacci Series upto 1000 using 'yield'.
def fibonacci_series(number)
@first_number = 0
@second_number = 1
while @first_number <= number
yield(@first_number, @second_number)
end