10/8/21

WEB TECHNOLOGIES LAB EXERCISE 7 to EXERCISE 14

 EXPERIMENT-7: 

AIM: Write Ruby program reads a number and calculates the factorial value of it and prints the Same. 

SOURCE CODE:

fact=1 

puts 'Enter the Number:' 

a=gets 

n=a.to_i 

for i in 1..n 

fact=fact*i 

end 

puts "Factorial= #{fact}"


EXPERIMENT-8: 

AIM: Write a Ruby program that counts the number of lines in a text file using its regular Expressions facility. 

SOURCE CODE:

puts 'Enter file name:' 

fname=gets.chomp; 

count=IO.readlines(fname).size; 

puts "There are #{count} lines in #{fname}"


EXPERIMENT-9: 

AIM: Write a Ruby program that uses iterator to find out the length of a string. 

SOURCE CODE: 

print "Enter a string:" 

str = gets.chomp 

c=0 

str.each_char do |i| 

c=c+1 

end 

puts "Length of string: #{c}"


EXPERIMENT-10: 

AIM: Write simple Ruby programs that uses arrays in Ruby. 

SOURCE CODE: 

nums = Array[1, 2, 3, 4,5] 

nums2=Array[6,7,8,9,10] 

puts "#{nums}" 

puts nums[0] 

puts nums.length 

puts nums[0..2] 

puts nums.slice(0,2) 

puts nums.at(4) 

puts nums.concat(nums2) 

puts nums.delete_at(0) 

puts "#{nums}" 

puts nums.empty? 

puts nums.eql?(nums2) 

puts nums.last 

puts "#{nums}" 

puts nums.pop

 puts "#{nums}" 

puts nums.push(10) 

puts nums.reverse 

puts nums.shift 

puts "#{nums}" 

puts nums.size

 puts nums.sort


EXPERIMENT-11: 

AIM: Write programs which uses associative arrays concept of Ruby. 

SOURCE CODE: 

#!/usr/bin/ruby

data = {"Akash" => "Physics", "Ankit" => "Chemistry", "Aman" => "Maths"} 

 puts data["Akash"] 

 puts data["Ankit"] 

 puts data["Aman"] 

puts data["Ankit"] 

data["Ankit"]="biology"

 puts data["Ankit"] 

data.clear 

puts data["Ankit"] 

data = {"Akash" => "Physics", "Ankit" => "Chemistry", "Aman" => "Maths"} 

 puts data["Ankit"] 

data.delete("Ankit") 

puts data["Ankit"] 

puts data.empty? 

puts data.fetch("Akash")

 puts data.invert 

puts data.length 

data2 = {"Ravi" => "Botany", "Ram" => "Biology"}

 puts data.merge(data2) 

puts data.replace(data2) 

puts data.shift 

puts data.size 

puts data.store("Ravi","Botany") 

puts data.size 

puts data.sor


EXPERIMENT-12: 

AIM: Write Ruby program which uses Math module to find area of a triangle. 

SOURCE CODE: 

puts("Enter a,b,c values:\n") 

a = gets.to_i 

b = gets.to_i 

c = gets.to_i 

s = (a+b+c)/2 

area = (Math.sqrt(s*(s-a)*(s-b)*(s-c)).round(2)) 

puts("Area of a triangle is #{area}")


EXPERIMENT-13: 

AIM: Write Ruby program which uses tk module to display a window. 

SOURCE CODE:

require 'tk' 

root = TkRoot.new { title "Hello, World!" } 

TkLabel.new(root) do

 text 'Hello, World!'

 pack { padx 15 ; pady 15; side 'left' } 

end 

Tk.mainloop


EXPERIMENT-14: 

AIM: Define complex class in Ruby and do write methods to carry operations on complex objects. 

SOURCE CODE:

class Cmplx 

attr_accessor :real, :image 

def read

 @real=gets.to_i 

@imag=gets.to_i 

end 

def add(other)

 ob = Cmplx.new 

ob.real = @real+other.real;

 ob.imag = @imag+other.imag; 

return ob

 end 

def subtract(other)

 ob = Cmplx.new; 

ob.real = @real-other.real; 

ob.imag = @imag-other.imag; 

return ob; 

end 

def multiply(other) 

ob = Cmplx.new

 ob.real = (@real * other.real)-(imag*other.imag); 

ob.imag = (@real * other.imag)+(imag*other.real); 

return ob; 

end 

def divide(other) 

t = Cmplx.new; 

ob = Cmplx.new; 

t.imag = -other.imag; 

r =(other.real).abs;

 i =(other.imag).abs; 

d =(r*r)+(i*i); 

ob.real = ((@real * other.real)-(imag*t.imag))/d; 

ob.imag = ((@real * t.imag)+(imag*other.real))/d;

 return ob; 

end

 def disp if @imag<0 

puts "#{@real}#{@imag}i" else

 puts "#{@real}+#{@imag}i" 

end 

end 

end 

t1 = Cmplx.new 

t2 = Cmplx.new 

t3 = Cmplx.new

 t4 = Cmplx.new 

t5 = Cmplx.new 

t6 = Cmplx.new 

puts "Enter first number" 

t1.read 

puts "Enter Second number" 

t2.read 

print "FIRST NUMBER:" 

t1.disp 

print "SECOND NUMBER:"

 t2.disp print "ADDITION:"

 t3 = t1.add(t2) 

t3.disp 

print "SUBTRACTION:"

 t4 = t1.subtract(t2)

 t4.disp 

print "MULTIPLICATION:"

 t5 = t1.multiply(t2) 

t5.disp print "DIVISION:"

 t6 = t1.divide(t2)

 t6.disp



1 comment:

basant kumar said...

factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.

Blog Archive