Rust program to extract the last two digits from a given year

Given a year, we have to extract the last two digits from a given year using the Rust program.
Submitted by Nidhi, on October 02, 2021

Problem Solution:

Here, we will read a year from the user. Then we will extract the last two digits from the given year.

Program/Source Code:

The source code to extract the last two digits from a given year is given below. The given program is compiled and executed successfully.

// Rust program to extract the 
// last two digits from a given year

use std::io;

fn main() 
{
    let mut year:i32  =0;
    let mut res:i32   =0;
    
    let mut input = String::new();
    
    println!("Enter year: ");
    io::stdin().read_line(&mut input).expect("Not a valid string");
    year = input.trim().parse().expect("Not a valid number");
    
    res = year % 100;

    println!("Result is: {}", res);
}

Output:

Enter year: 
2021
Result is: 21

Explanation:

Here, we read the year from the user. After that, we extracted the last two digits from the given year and printed the result.

Rust Basic Programs »



Related Programs




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.