Posts

Showing posts from April, 2015

Bubble sort program in Ruby

Image
Bubble sort program in Ruby Step-by-step example Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort. In each step, elements written in   bold   are being compared. Three passes will be required. First Pass: (  5   1  4 2 8 )   (  1   5  4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1. ( 1  5   4  2 8 )   ( 1  4   5  2 8 ), Swap since 5 > 4 ( 1 4  5   2  8 )   ( 1 4  2   5  8 ), Swap since 5 > 2 ( 1 4 2  5   8  )   ( 1 4 2  5   8  ), Now, since these elements are already in order (8 > 5), algorithm does not swap them. Second Pass: (  1   4  2 5 8 )   (  1   4  2 5 8 ) ( 1  4   2  5 8 )   ( 1  2   4  5 8 ), Swap since 4 ...

Employee payroll details program in Ruby

#Employee payroll Details module Employee     def details       puts "Enter Employee Id"       @emp_id=gets.to_i       puts "Enter Employee Name"       @emp_name=gets       puts "Enter Date of Birth"       date=gets.to_i       puts "Ente the Employee Salary"       @emp_salary=gets.to_i     end end module Salary     def salaries       @da = @emp_salary*15/100       @hra = @emp_salary*10/100       @pf = @emp_salary*12/100       @esi = @emp_salary*2/100       @deduction=@pf + @esi       @gross_sal=@emp_salary + @da + @hra       @net_sal=@gross_sal- @deduction         puts "\nEmployee Id = #{@emp_id}"         puts "Employee Name = #{@emp_name}"         puts...

fibonacci series in Ruby

The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... The next number is found by adding up the two numbers before it. The 2 is found by adding the two numbers before it (1+1) Similarly, the 3 is found by adding the two numbers before it (1+2), And the 5 is (2+3), and so on! Fibonacci series in Ruby class Fibonacci   def series     puts "Enter the Fibonacci value"     n=gets.to_i     f1=0     f2=1     f3=0         while f3<n do            f3=f1+f2           puts f3           f1=f2           f2=f3          end     end end obj1=Fibonacci.new obj1.series

Factorial of a number in Ruby

A.Factorials   are very simple things. They're just products, indicated by an exclamation mark. For instance, "four   factorial " is written as "4!" and means 1×2×3×4 = 24. In general, n! ("n   factorial ") means the product of all the whole numbers from 1 to n; that is, n! = 1×2×3×...×n. Factorial of a number in Ruby #!/usr/bin/ruby puts "Enter a Number" n=gets.to_i fact=1 for i in 1..n fact=fact*i end puts fact. Out put: Enter a Number 5 120

Prime number program in Ruby

A Prime Number can be divided evenly only by 1, or itself.  And it must be a whole number greater than 1.  Example: 5 can only be divided evenly by 1 or 5, so it is a prime number. But 6 can be divided evenly by 1, 2, 3 and 6 so it is NOT a prime number (it is a composite number). Prime number Program in Ruby #!/usr/bin/ruby class Prime def cal puts "Enter a number" a=gets.to_i @@flag=0 for i in 2..a-1 if a%i==0 @@flag=1 end if @@flag==0 puts "Prime" break else puts "not prime" break end end end end b=Prime.new b.cal

Armstrong number program in Ruby

What is Armstrong Number?? An  Armstrong number  of three digits is an integer such that the sum of the cubes of its digits is equal to the  number  itself. For example, 371 is an  Armstrong number  since 3**3 + 7**3 + 1**3 = 371.  Armstrong Number Program in Ruby          #!/usr/bin/ruby             class Armstrong        puts "Enter a number"                      number=STDIN.gets.to_i               sum=0                       d=number           while (d!=0)     sum=sum+(d%10)*(d%10)*(d%10)      d=d/10    end    if(sum==number) puts "your number is Armstron...