I am trying to create a program that prompts a user to input a number and then checks if its a prime number. I am also trying to get it to display the factors if it isn't a prime number.
I managed to create the first part of the program, but I am struggling with the last part.
def prime(n)
is_prime = true
for i in 2..n-1
if n % i == 0
is_prime = false
end
end
if is_prime
puts "#{n} is a prime number"
else
puts "#{n} is not a prime number =>"
end
end
prime(n)
At this step:
puts "#{n} is not a prime number =>"
I want to incorporate the displaying of factors, let's say the number is 8
8 is not a prime number => 1, 2, 4, 8
Any help or advice would be greatly appreciated!