Showing posts with label ruby programming. Show all posts
Showing posts with label ruby programming. Show all posts

Ruby Code Examples



Here I will provide Ruby Code Examples for all basic ruby programs of String, Array, Logical programs and Number based programs. In Ruby programming language.

Array Based Logical Programs


Ruby program to remove duplicate values in array

Program to find the second smallest number in an array in ruby

Find second largest number from an array in Ruby

Find smallest element in array without using min function in Ruby

Find largest element in array without using max function in Ruby

String Based Logical Programs

Ruby program to remove duplicate characters of a string

Ruby program to find occurrence of character in string

Find smallest and longest substring of a string in Ruby

Ruby program to reverse string without using direct method

Ruby program to convert full name into initials

Ruby program to remove common characters from two strings

Ruby program to remove vowels from string

Ruby program to search a string in a sentence

Number Based Logical Programs

Strong number program in Ruby


Prime number program in Ruby

Write a Ruby program to check the armstrong number

Ruby program for random number between two numbers

Ruby program to check Palindrome Number

Ruby program to reverse a number

Ruby program to sum of digits of a number

Ruby program to calculate generic root

Write a Ruby program to swap two arrays

- Ruby program to split number into digits

- Ruby program to print perfect numbers from 1 to 1000

- Ruby program to check perfect number


Ruby program to split number into digits



Write a Ruby program to split number into digits

class NumberToDigits
  def printNumberToDigit
    print "Enter the number "
    num = gets.chomp
    print "Digits are: "
    num.split("").each do |n|
      print n+ " "
    end
  end
end

p = NumberToDigit.new 
p.printNumberToDigits

Output:


  
Enter the number  589
Digits are: 5 8 9 


Ruby program to print perfect numbers from 1 to 1000



Write Ruby program to print perfect numbers from 1 to 1000

Perfect Number:  a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.

class PerfectNumber
  def printPerfectNumbers
    i=1
    sum=0
    (1..1000).each do |n|
      i = 1
      sum = 0
      while i < n do 
        sum = (sum + i) if n % i == 0
        i = i + 1
      end
      puts n if sum == n
    end
  end
end

p = PerfectNumber.new 
p.printPerfectNumbers

Output:

  
6
28
496


Ruby program to check perfect number



Write a Ruby program to check perfect number

Perfect Number:  a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself.

class PerfectNumber
  def checkPerfectNumber
    i=1
    sum=0
    puts "Enter the number"
    n = gets.chomp.to_i
    while i < n do 
      sum = (sum + i) if n % i == 0
      i = i + 1
    end
    if sum == n
      puts "Given number is perfect number"
    else
      puts "Given number is not a perfect number"
    end
  end
end

p = PerfectNumber.new 
p.checkPerfectNumber


Output:


Enter the number
 6
Given number is perfect number
=> ni