Control flow is a fundamental concept in programming that allows you to dictate how your code executes based on certain conditions. In Rust, one of the ways to control the flow of your program is by using if-else
statements.
In this challenge, you will implement a function that checks whether a number is positive, negative, or zero. Depending on the value, your function should return a corresponding string message.
Your task is to complete the function check_number_sign
that takes an integer i32
as input and returns a String
indicating whether the number is "positive"
, "negative"
, or "zero"
.
"positive"
."negative"
."zero"
.if
, else-if
, and else
keywords to implement the control flow.String
.hbdo
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. let mut result = ""; if number > 0 { result = "positive"; } // Step 2: // Check if the number is negative. if number < 0 { result = "negative"; } // Step 3: // Handle the case where it's neither positive nor negative. if number == 0 { result = "zero"; } String::from(result)}
infamous-711
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative. if number > 0 { String::from("positive") } else if number < 0 { String::from("negative") } else { // if the above conditions are false, we know for sure it is zero String::from("zero") }}
jhq223
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative. if number>0{ "positive".to_string() } else if number < 0{ "negative".to_string() } else{ "zero".to_string() }}
ThatphumCpre
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { return String::from("positive"); } else if number < 0 { return String::from("negative"); } else { String::from("zero") }}
francisco-cmyk
pub fn check_number_sign(number: i32) -> String { if number > 0 { return "positive".to_string() } else if number < 0 { return "negative".to_string() } else { return "zero".to_string() }}
xbarnett
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative. String::from(if number < 0 { "negative" } else if (number == 0) { "zero" } else { "positive" })}
carlos-quantexai
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { "positive".to_string() } else if number < 0 { "negative".to_string() } else { "zero".to_string() } // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative.}
mk-comm
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative. if number == 0 { return "zero".to_string() } else if number > 0 { return "positive".to_string() } else { return "negative".to_string() }}
MosesMp3
pub fn check_number_sign(number: i32) -> String { if number >0{ return "positive".to_string(); } // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. else if number <0{ return "negative".to_string(); } // Return `"zero"` if the number is zero. else{ return "zero".to_string(); } // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative.}
nordicmachine
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { return String::from("positive") } // Step 2: // Check if the number is negative. else if number < 0 { return String::from("negative") } // Step 3: // Handle the case where it's neither positive nor negative. else { return String::from("zero") }}
majesticalcreature
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { return String::from("positive"); } // Step 2: // Check if the number is negative. if number < 0 { return String::from("negative"); } // Step 3: // Handle the case where it's neither positive nor negative. String::from("zero")}
josschne
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 {return "positive".to_string()} // Step 2: // Check if the number is negative. if number < 0 {return "negative".to_string()} // Step 3: // Handle the case where it's neither positive nor negative. "zero".to_string()}
sander-b-postnl
pub fn check_number_sign(number: i32) -> String { let sign = match number { n if n > 0 => "positive", n if n < 0 => "negative", _ => "zero", }; sign.to_string()}
matei
pub fn check_number_sign(number: i32) -> String { if number > 0 { String::from("positive") } else if number < 0 { String::from("negative") } else { String::from("zero") } // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative.}
st01011
pub fn check_number_sign(number: i32) -> String { if number > 0{ return String::from("positive") } else if number < 0 { return String::from("negative") } else { return String::from("zero") } }
hendrikdevloed
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 {return "positive".to_string()} // Step 2: // Check if the number is negative. if number < 0 {return "negative".to_string()} // Step 3: // Handle the case where it's neither positive nor negative. String::from("zero")}
rot11
pub fn check_number_sign(number: i32) -> String { if number > 0 { "positive".to_string() } else if number < 0 { "negative".to_string() } else { "zero".to_string() }}
amassare
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative. String::from(if number>0{ "positive" }else if number==0{ "zero" }else{ "negative" })}
edoardo-morosanu
pub fn check_number_sign(number: i32) -> String { if number > 0 { String::from("positive") } else if number < 0 { String::from("negative") } else { String::from("zero") }}
edoardo-morosanu
pub fn check_number_sign(number: i32) -> String { if number > 0 { String::from("positive") } else if number < 0 { String::from("negative") } else { String::from("zero") }}
edoardo-morosanu
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. if number > 0 { String::from("positive") } else if number < 0 { String::from("negative") } else { String::from("zero") }}
zavakid
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. if number > 0 { return "positive".into(); } else // Return `"negative"` if the number is negative. if number < 0 { return "negative".into(); } else // Return `"zero"` if the number is zero. {return "zero".into(); } // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative.}
victorchukwuemeka
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative. if number > 0 { return String::from("positive"); }else if number < 0{ return String::from("negative"); }else{ return String::from("zero"); }}
Johnchoi913
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative. match number { x if x > 0 => { "positive".to_string() } x if x < 0 => { "negative".to_string() } _ => { "zero".to_string() } }}
Parallaxes
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { String::from("positive") } else if number < 0 { String::from("negative") } else { String::from("zero") } // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative.}
brum-b0
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. println!("{}", number); // Step 1: // Check if the number is positive. if number > 0 { String::from("positive") } else if number < 0 { String::from("negative") } else { String::from("zero") } // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative. }
tiagombsilva
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { return String::from("positive") }else if number < 0 { return String::from("negative"); } else { return String::from("zero") } // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative.}
i5-650
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { "positive".to_string() } else if number < 0 { "negative".to_string() } else { "zero".to_string() } // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative.}
retinotopic
pub fn check_number_sign(number: i32) -> String { let sign: String; if number > 0 { sign = String::from("positive"); } else if number < 0 { sign = String::from("negative"); } else { sign = String::from("zero"); } // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. sign // Step 3: // Handle the case where it's neither positive nor negative.}
hhornbacher
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number.is_positive() {return "positive".into();} // Step 2: // Check if the number is negative. if number.is_negative() {return "negative".into();} // Step 3: // Handle the case where it's neither positive nor negative. "zero".into()}
ogaca42
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. (if number > 0 { "positive" } else if number < 0 { "negative" } else { "zero" }).to_string() // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative.}
st01011
pub fn check_number_sign(number: i32) -> String { if number > 0 { return "positive".to_string() } else if number < 0 { return "negative".to_string() } else { return "zero".to_string() } }
Ustin
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 {return "positive".to_string();} // Step 2: // Check if the number is negative. if number < 0 {return "negative".to_string();} // Step 3: // Handle the case where it's neither positive nor negative. "zero".to_string()}
pbjarterot
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { return String::from("positive"); } // Step 2: // Check if the number is negative. if number < 0 { return String::from("negative"); } // Step 3: // Handle the case where it's neither positive nor negative. String::from("zero") }
otherJL0
pub fn check_number_sign(number: i32) -> String { if number > 0 { return String::from("positive"); } if number < 0 { return String::from("negative"); } String::from("zero")}
CianciuStyles
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { return "positive".to_string(); } // Step 2: // Check if the number is negative. if number < 0 { return "negative".to_string(); } // Step 3: // Handle the case where it's neither positive nor negative. "zero".to_string()}
fuuman
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { return String::from("positive"); } // Step 2: // Check if the number is negative. if number < 0 { return String::from("negative"); } // Step 3: // Handle the case where it's neither positive nor negative. String::from("zero")}
ayushrawat10
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. match number { 0 => { "zero".to_owned() }, 1.. => { "positive".to_owned() }, ..=-1 => { "negative".to_owned() }, } // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative.}
Araden14
pub fn check_number_sign(number: i32) -> String { let mut result = String::new(); if number == 0 { result.push_str("zero"); } else if number > 0 { result.push_str("positive"); } else { result.push_str("negative"); } println!("{}", result); result }
damascussteel21
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { String::from("positive") } // Step 2: // Check if the number is negative. else if number < 0 { String::from("negative") } // Step 3: // Handle the case where it's neither positive nor negative. else { String::from("zero") }}
agegorin
pub fn check_number_sign(number: i32) -> String { if number > 0 { return "positive".to_string(); } if number < 0 { return "negative".to_string(); } return "zero".to_string();}
daanbouwman19
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { return "positive".to_string(); } // Step 2: // Check if the number is negative. if number < 0 { return "negative".to_string(); } // Step 3: // Handle the case where it's neither positive nor negative. "zero".to_string()}
alexromanov
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { return String::from("positive"); } else if number < 0 { return String::from("negative"); } else { return String::from("zero"); } // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative.}
LaurentiuStoleriu
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. if number > 0 { return String::from("positive"); } // Step 1: // Check if the number is positive. if number < 0 { return String::from("negative"); } String::from("zero")}
Timepatterns
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { "positive".to_string() } else if number < 0 { "negative".to_string() } else { "zero".to_string() } // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative.}
dev-puzzler
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. let mut result = ""; // Step 1: // Check if the number is positive. if number > 0 { result = "positive"; } // Step 2: // Check if the number is negative. if number < 0 { result = "negative"; } // Step 3: // Handle the case where it's neither positive nor negative. if number == 0 { result = "zero"; } return result.to_string();}
oneopane
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. if number > 0 { String::from("positive") } else if number < 0 { String::from("negative") } else { String::from("zero") } // Step 1: // Check if the number is positive. // Step 2: // Check if the number is negative. // Step 3: // Handle the case where it's neither positive nor negative.}
bmelancon
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 {"positive".to_string()} // Step 2: // Check if the number is negative. else if number < 0 {"negative".to_string()} // Step 3: // Handle the case where it's neither positive nor negative. else {"zero".to_string()}}
yurm04
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { String::from("positive") } else if number < 0 { // Step 2: // Check if the number is negative. String::from("negative") } else { // Step 3: // Handle the case where it's neither positive nor negative. String::from("zero") }}
Xinoi
pub fn check_number_sign(number: i32) -> String { // Return `"positive"` if the number is positive. // Return `"negative"` if the number is negative. // Return `"zero"` if the number is zero. // Step 1: // Check if the number is positive. if number > 0 { "positive".to_string() } // Step 2: // Check if the number is negative. else if number < 0 { "negative".to_string() } // Step 3: // Handle the case where it's neither positive nor negative. else { "zero".to_string() }}