Q:

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

belongs to collection: Rust Basic Programs

0

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

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 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.

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 find the roots of a quadratic equa... >>
<< Rust program to check whether a given character is...