how to declare a input number positive or negative? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

how to declare a input number positive or negative?

If input>0 = positive, x<0 negative, x=0 is zero...... Help me down

17th Apr 2020, 3:45 AM
Md Rayhan Kabir
Md Rayhan Kabir - avatar
6 Answers
+ 2
There is nothing wrong with the comparison logic by itself. All that has to be changed is to compare x.to_i with 0 instead of just normal x since gets.chomp by default receives input as strings so you have to cast the string to an integer with the str.to_i method to make comparisons with an integer.
17th Apr 2020, 5:28 AM
Michael David
Michael David - avatar
+ 1
Declaration doesn’t change for a positive or negative number, and yeah for “if” conditions, that is how you would check. Idk if that was what you were asking for. This question was kind of worded unclearly. Are you learning Ruby because of Shopify too?
17th Apr 2020, 4:03 AM
Michael David
Michael David - avatar
+ 1
Michael David I am a new learner of Ruby programming. x= gets.chomp If x>0 Puts "positive" else Puts "negative" end Please correct my code...
17th Apr 2020, 4:16 AM
Md Rayhan Kabir
Md Rayhan Kabir - avatar
+ 1
Here is the solution x= gets.chomp If x.to_i > 0 Puts "positive" else Puts "negative" end
17th Apr 2020, 8:35 PM
prashanth
0
prashanth Run error 😢😢
18th Apr 2020, 2:12 AM
Md Rayhan Kabir
Md Rayhan Kabir - avatar
0
I think it was indentation that was the problem.
18th Apr 2020, 4:30 AM
Michael David
Michael David - avatar