How to check if value is negative or positive?

i want to check user input value if its negative so i will print you enter negative value if its positive i will print its positive... by if else..

What's the problem? If you have a number, you can compare it to zero like to any other number, using comparison operators <, >, <=, >=.

if number > 0 {
  println!("You enter positive value.");
}else{
  println!("You enter negative value");
}

like this

Looks OK. It doesn't compile, or there is another problem?

what is the problem

In other words, what are you trying to ask? What doesn't work if you simply paste this piece of code into your program?

i am doing a assignment so question is.

write a function that receives one argument of any suitable data type and print whether the number is positive or negative using if-else conditions

Then everything to solve this should already have been taught in the course.

2 Likes

I'm curious:

Is there really a place where they are teaching introductory programming using Rust?

That's great!

So, what you're trying to do and what doesn't work as expected?

I think you should start by reading the book. In chapter 2 they explain how to write a guessing game, so something similar to yours?

It seems, that you have little to no experience in Rust. I think the book is an excellent starting place to learn Rust. A lot of people had put a lot of thinking into it. You will love it (hopefully). :heart:

1 Like

Hi @faizanff! Additionally to all the great answers given already, I want to add the following:

  1. any suitable data type: This seems to suggest to use generics.
  2. Make sure what the definition of "positive" and "negative" is according to the teacher. Technically, 0 is neither positive nor negative. Check to find out if you should implement 0 as a special case.

Actually, I think maybe I misunderstood. any suitable data type can also mean you can choose for which concrete type you implement it, as long as it makes sense. If you didn't learn about generics yet in the course, this is probably the correct interpretation.

Anyway, I implemented a generic solution: Playground

In other languages they made predefind functions for checking data type and is_empty, is_numric functions so I am new here in rust so that's why I got problem..

But I solve it

Code:

let x = 30;
if x > 0{
  println!("value is positive");
}else{
   println!("Value is negative");
}

Thanks guys for support..

I'm not sure whether this is good, since I can't pass into your function, for example, u8.

"In other languages" - you mean dynamically-typed languages, like Python? Then you'll have to learn the static typing, I'm afraid, since it is a lot different.

Also keep in mind that the term "positive" often includes 0, so you may want to use x >= 0 instead

Oh yeah. Then it seems you'd have to implement your own Number marker trait for [ u | i ] [ 8 | 16 | 32 | 64 | 128 ] and f [ 32 | 64 ] (and for bignums if you need to) and bound the input on the trait.