Arithmetic/Integer: Difference between revisions

Content added Content deleted
Line 2,715: Line 2,715:


Note that this code cannot be run within the [http://play.rust-lang.org Rust playpen] as it does not support console input.
Note that this code cannot be run within the [http://play.rust-lang.org Rust playpen] as it does not support console input.
<lang rust>use std::io;
<lang rust>use std::env;


fn main() {
fn main() {
let args: Vec<_> = env::args().collect();
#![allow(unstable)] // Currently required whilst Rust 1.0 is finalised
let a: i32 = from_str(io::stdin().read_line().unwrap().trim().as_slice()).unwrap();
let a = args[1].parse::<i32>().unwrap();
let b: i32 = from_str(io::stdin().read_line().unwrap().trim().as_slice()).unwrap();
let b = args[2].parse::<i32>().unwrap();


let sum = a + b;
println!("sum: {}", a + b);
println!("a + b = {0}" , sum);
println!("difference: {}", a - b);
println!("a - b = {0}" , a - b);
println!("product: {}", a * b);
println!("a * b = {0}" , a * b);
println!("integer quotient: {}", a / b); // truncates towards zero
println!("quotient of a / b = {0}" , a / b); // truncates towards 0
println!("remainder: {}", a % b); // same sign as first operand
println!("remainder of a / b = {0}" , a % b); // same sign as first operand
}</lang>
}</lang>