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

Comments

Popular posts from this blog

how to customize dashboard in active admin gem in rails 4

fibonacci series in Ruby

Write a program to Sum of Digits in JQuery?