Showing posts with label prime number generator in ruby. Show all posts
Showing posts with label prime number generator in ruby. Show all posts

Thursday, October 20, 2016

Prime Numbers in Ruby

Here is a simple program that will ask the user to give a number and then our program will generate the prime numbers based on the given number by our user using Ruby as our programming language.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com. 

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com. 

My mobile number here in the Philippines is 09173084360.


Program Listing

prime_number.rb

  def get_prime_no_upto(number)
  start = 2
  primes = (start..number).to_a
  (start..number).each do |no|
    (start..no).each do |num|
      if ( no % num  == 0) && num != no
        primes.delete(no)
        break
      end
    end
  end
  primes
end

loop do
print "\n\n";
print "PRIME Numbers"
print "\n\n";
print "Enter a Number : ";
num_test = gets.chomp
print "\n\n";
print "List of Prime Numbers";
print "\n\n";
print get_prime_no_upto(num_test.to_i)
puts "\n\n";
print "Do you want to continue? (y/n) : "
  answer = gets.chomp.downcase
  if answer == "n"
print "\n\n";
    print "End of Program";
    print "\n\n";
break
  end
end