Browse Source

Ch03 Homework

master
Saura Sambit 1 year ago
parent
commit
3fefc1a873
  1. 10
      README.md
  2. 6
      ch03/hw/farcel/Cargo.lock
  3. 9
      ch03/hw/farcel/Cargo.toml
  4. 11
      ch03/hw/farcel/src/main.rs
  5. 6
      ch03/hw/fibonacci/Cargo.lock
  6. 9
      ch03/hw/fibonacci/Cargo.toml
  7. 33
      ch03/hw/fibonacci/src/main.rs

10
README.md

@ -12,6 +12,10 @@ cargo run
######I/O Binary
```
use std::io;
//take user input
let mut far = String::new();
io::stdin().read_line(&mut far).expect("Failed to read line.");
```
######Program entry point
```
@ -168,6 +172,12 @@ let result = loop {
}
};
```
For loop:
```
for _i in 1..10 { //
//statements to be repeated
}
```
Looping through each element of a collection using a `for` loop:
```
let a = [10, 20, 30, 40, 50];

6
ch03/hw/farcel/Cargo.lock

@ -0,0 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "farcel"
version = "0.1.0"

9
ch03/hw/farcel/Cargo.toml

@ -0,0 +1,9 @@
[package]
name = "farcel"
version = "0.1.0"
authors = ["Saura Sambit <sasach@riseup.net>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

11
ch03/hw/farcel/src/main.rs

@ -0,0 +1,11 @@
use std::io;
fn main() {
println!("Enter temperature in Fahrenheit:");
let mut far = String::new();
io::stdin().read_line(&mut far).expect("Failed to read line.");
let far: f64 = far.trim()
.parse()
.expect("Please type a number.");
let cel: f64 = (5.0*far)/9.0 - 32.0;
println!("Celsius: {}", cel);
}

6
ch03/hw/fibonacci/Cargo.lock

@ -0,0 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "fibonacci"
version = "0.1.0"

9
ch03/hw/fibonacci/Cargo.toml

@ -0,0 +1,9 @@
[package]
name = "fibonacci"
version = "0.1.0"
authors = ["Saura Sambit <sasach@riseup.net>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

33
ch03/hw/fibonacci/src/main.rs

@ -0,0 +1,33 @@
use std::io;
fn main() {
println!("Enter the value of n:");
let mut n = String::new();
io::stdin().read_line(&mut n).expect("Failed to read line.");
let n: u32 = n.trim()
.parse()
.expect("Please type a number.");
let ans = fibonacci(n);
/*
let mut a = 0;
let mut b = 1;
let mut c = 0;
for _i in 2..n {
c = a+b;
a = b;
b = c;
}
println!("nth Fibonacci number: {}", c);
*/
println!("nth Fibonacci number is: {}", ans);
}
fn fibonacci(n: u32) -> u32 {
if n == 2 {
return 1;
}
else if n == 1 {
return 0;
}
else {
return fibonacci(n-1) + fibonacci(n-2);
}
}
Loading…
Cancel
Save