A simple program that will compute the average value of five numbers that is being provided by the user using Rust Programming language.
Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
Sample Program Output
Program Listing
average.js
// Average of Number Program in Rust Programming language.
// Written By: Mr. Jake R. Pomperada, MAED-IT
// July 17, 2016
use std::io::{self, Write};
use std::fmt::Display;
use std::process;
fn main() {
println!("\n");
println!("\tAverage of Number Program in Rust");
println!("\n");
let num1 : i32 = grab_input("Enter Value No. 1 ")
.unwrap_or_else(|e| exit_err(&e, e.raw_os_error().unwrap_or(-1)))
.trim()
.parse()
.unwrap_or_else(|e| exit_err(&e, 2));
let num2 : i32 = grab_input("Enter Value No. 2 ")
.unwrap_or_else(|e| exit_err(&e, e.raw_os_error().unwrap_or(-1)))
.trim()
.parse()
.unwrap_or_else(|e| exit_err(&e, 2));
let num3 : i32 = grab_input("Enter Value No. 3 ")
.unwrap_or_else(|e| exit_err(&e, e.raw_os_error().unwrap_or(-1)))
.trim()
.parse()
.unwrap_or_else(|e| exit_err(&e, 2));
let num4 : i32 = grab_input("Enter Value No. 4 ")
.unwrap_or_else(|e| exit_err(&e, e.raw_os_error().unwrap_or(-1)))
.trim()
.parse()
.unwrap_or_else(|e| exit_err(&e, 2));
let num5 : i32 = grab_input("Enter Value No. 5 ")
.unwrap_or_else(|e| exit_err(&e, e.raw_os_error().unwrap_or(-1)))
.trim()
.parse()
.unwrap_or_else(|e| exit_err(&e, 2));
let solve_average = (num1+num2+num3+num4+num5) / 5;
println!("\n");
println!("The average of {},{},{},{} and {} is {}."
,num1,num2,num3,num4,num5,solve_average);
println!("\n");
println!("End of Program");
}
fn grab_input(msg: &str) -> io::Result<String> {
let mut buf = String::new();
print!("{}: ", msg);
try!(io::stdout().flush());
try!(io::stdin().read_line(&mut buf));
Ok(buf)
}
fn exit_err<T: Display>(msg: T, code: i32) -> ! {
let _ = writeln!(&mut io::stderr(), "Error: {}", msg);
process::exit(code)
}
No comments:
Post a Comment