4
2

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.

偶数か奇数か

Last updated at Posted at 2020-05-28

偶数か奇数か判定するプログラム

言語習得の初期によく出てくる偶数か奇数かを判定するプログラムを改めて。

###プログラム
#####python

pythonevenoroddjudge.py
def is_even(num):
    return num % 2 == 0


num = float(input())

print(is_even(num))

#####julia

juliaevenoroddjudge.jl
function is_even(num)
    return num % 2 == 0
end
function asknumber()
    print("Enter a number: ")
    parse(Float64, readline())
end
num = asknumber()
is_even(num)

juliaのコードではわざわざfunctionを使っているがif文でも書ける。それについてはまた違う記事で。

rust
use std::io;
use std::io::Write;
use std::io::stdout;
 
fn main() {
 
    println!("input number : ");
        stdout().flush().unwrap();
 
    let mut number = String::new();
 
    io::stdin().read_line(&mut number)
        .expect("failured!");
 
    let number: u32 = number.trim().parse()
        .expect("input number");
 
    if number % 2 == 0 {
        println!("{} is even",number);
    } else {
        println!("{} is odd",number);
    }
}

ほかの言語も後で書きます。

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?