Sunday, July 17, 2016

Simple Interest Rate Solver in Rust

A simple program that will compute the loan of the person based on the principal amount loan, interest rate and time duration of the loan using Rust as my 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

interest.rs

// Simple InterestProgram 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!("\tSimple Interest Program in Rust");
       println!("\n");

       
    let  principal: f32 = grab_input("Enter Principal Amount $ ")
        .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  rate: f32 = grab_input("Enter Rate of Interest $ ")
        .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  time: f32 = grab_input("Enter Period of Time  ")
        .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_interest = (principal * rate * time) / 100.0;
              
    println!("\n"); 
    println!("The simple interest is {:.2}.",solve_interest);
    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