ADVERTISEMENT
ADVERTISEMENT

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.