-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request: New challenge: If else #7
- Loading branch information
Showing
9 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "if-else" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Control flow is a fundamental concept in any programming language, including Rust. The `if-else` statement is a basic control flow construct that allows you to execute different code blocks based on certain conditions. | ||
|
||
## Your task | ||
|
||
In this challenge, you will write a function `is_even(n: i32) -> bool` that checks if a given number is even or odd using an `if-else` statement. The function should return `true` if the number is even, and `false` if it is odd. | ||
|
||
## Requirements | ||
|
||
- The function should take an integer as input. | ||
- Use an `if-else` statement to determine if the number is even. | ||
- Return `true` if the number is even. | ||
- Return `false` if the number is odd. | ||
|
||
## Example | ||
|
||
```rust | ||
assert_eq!(is_even(4), true); | ||
assert_eq!(is_even(7), false); | ||
assert_eq!(is_even(0), true); | ||
assert_eq!(is_even(-2), true); | ||
assert_eq!(is_even(-3), false); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
mod tests; | ||
|
||
pub fn is_even(n: i32) -> bool { | ||
if n % 2 == 0 { | ||
true | ||
} else { | ||
false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub fn is_even(n: i32) -> bool { | ||
// Your code here... | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::is_even; | ||
|
||
#[test] | ||
fn test_is_even() { | ||
assert_eq!(is_even(4), true); | ||
assert_eq!(is_even(7), false); | ||
assert_eq!(is_even(0), true); | ||
assert_eq!(is_even(-2), true); | ||
assert_eq!(is_even(-3), false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters