Q:

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

belongs to collection: Rust Basic Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to read the height of a person and th... >>
<< Rust program to calculate the value of nPr...