Rust Program to Check the birthday and print Happy Birthday message


Check the birthday and print Happy Birthday message

Rust Programming Language


Check the birthday and print Happy Birthday message


Problem


Rust program that checks whether today is someone's birthday, and prints a happy birthday message if it is.

Input


use chrono::{Datelike, Local};

fn main() {
    // Change these values to the person's birthdate
    let birth_month = 4;
    let birth_day = 7;

    let today = Local::today();
    let today_month = today.month();
    let today_day = today.day();

    if birth_month == today_month && birth_day == today_day {
        println!("Happy birthday!");
    } else {
        println!("Today is not your birthday :(");
    }
}{codeBox}


Output


The output of this Rust program will depend on the current date and the birthdate specified in the program. If today is the person's birthday, the program will output:

Happy birthday!{codeBox}

If today is not the person's birthday, the program will output:

Today is not your birthday :({codeBox}

For example, if the birth month is 4 and the birth day is 7, and the program is run on April 7th, the output will be:

Happy birthday!{codeBox}

If the program is run on any other day of the year, the output will be:

Today is not your birthday :({codeBox}


Explanation


In this program, we use the chrono crate to get the current date using the Local::today() method. We then compare the month and day of the current date to the birth month and day specified in the program. If they match, we print a happy birthday message; otherwise, we print a message indicating that today is not the person's birthday.

Note that in this program, we use hard-coded values for the birth month and day, but you can easily modify the program to prompt the user for their birthdate or read it from a file or database.

Post a Comment