1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rust while を使ったプログラム

Last updated at Posted at 2020-06-17

Rustのドキュメントのフィボナッチ整数のn番目を求めるプログラムを書きました。

もしあなたがrustに関して困っているときに、このプログラムが役に立つのならばぜひ使ってください!
このプログラムにミスやもっといい方法があるならコメントをしてください!

use std::io;
//development pre complete 
fn main() {
  let mut num = String::new();
  io::stdin().read_line(&mut num).expect("err");
  let num:i64 = num.trim().parse().unwrap();

  if num == 1 {
    println!("{}",num);
  } else if num == 2 {
    println!("1");
  } else if num < 0 {
    println!("0以上の数字を入力してください。")
  } else {
    let mut time = 2;
    let mut one = 1;
    let mut two = 1;
    let mut tmp = 0;
    while time != num {
      tmp = one + two;
      one = two;
      two = tmp;
      time = time + 1;
    }
    println!("{}",tmp);
  }
}
1
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?