Rust program to convert a given number of days into days, weeks, and years

Given a number of days, we have to convert it into days, weeks, and years using Rust program.
Submitted by Nidhi, on September 29, 2021

Problem Solution:

Here, we will read the number of days from the user and convert it into years, weeks, and days.

Program/Source Code:

The source code to convert a given number of days into days, weeks, and years is given below. The given program is compiled and executed successfully.

// Rust program to convert a given number of days 
// into days, weeks, and years

use std::io;

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

    years = ndays / 365;
    weeks = (ndays % 365) / 7;
    days = (ndays % 365) % 7;

    println!("{} years, {} weeks and {} days", years, weeks, days);
}

Output:

RUN 1:
Enter days: 
471
1 years, 15 weeks and 1 days

RUN 2:
Enter days: 
1008
2 years, 39 weeks and 5 days

Explanation:

Here, we read the total number of days from the user. Then we find the years, weeks, and days. After that, we printed the result.

Rust Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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