Open In App

Program to convert weeks to days

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given no of weeks. Write a program to convert weeks to days.

Examples:

Input: weeks = 4
Output: 28 days

Input: weeks = 2
Output: 14 days

Approach: To solve the problem, follow the below idea:

Since we know a week has 7 days. So, to find no of days in a week, we multiply weeks by 7.

1 week = 7 days
n weeks = n*7 days

Below is the implementation of the above approach:

C++




#include <iostream>
using namespace std;
 
int convertWeeksToDays(int weeks)
{
    if (weeks < 0)
        return -1;
    // Convert weeks to days (1 week = 7 days)
    return weeks * 7;
}
 
int main()
{
    int weeks = 9;
 
    int days = convertWeeksToDays(weeks);
 
    if (days == -1) {
        cout << "Invalid Input" << endl;
    }
    else {
        // Display the result
        cout << weeks << " weeks is equal to " << days
             << " days." << endl;
    }
 
    return 0;
}


Java




public class WeekToDayConverter {
 
    // Function to convert weeks to days
    public static int convertWeeksToDays(int weeks) {
        // Check for invalid input (negative weeks)
        if (weeks < 0) {
            return -1;
        }
 
        // Convert weeks to days (1 week = 7 days)
        return weeks * 7;
    }
 
    public static void main(String[] args) {
        // Given weeks
        int weeks = 9;
 
        // Call the function to convert weeks to days
        int days = convertWeeksToDays(weeks);
 
        // Check for invalid input and display the result
        if (days == -1) {
            System.out.println("Invalid Input");
        } else {
            System.out.println(weeks + " weeks is equal to " + days + " days.");
        }
    }
}


Python3




def convert_weeks_to_days(weeks):
    # Check for invalid input (negative weeks)
    if weeks < 0:
        return -1
     
    # Convert weeks to days (1 week = 7 days)
    return weeks * 7
 
if __name__ == "__main__":
    # Given weeks
    weeks = 9
 
    # Call the function to convert weeks to days
    days = convert_weeks_to_days(weeks)
 
    # Check for invalid input and display the result
    if days == -1:
        print("Invalid Input")
    else:
        print(f"{weeks} weeks is equal to {days} days.")


C#




using System;
 
class Program
{
    // Function to convert weeks to days
    static int ConvertWeeksToDays(int weeks)
    {
        if (weeks < 0)
            return -1;
 
        // Convert weeks to days (1 week = 7 days)
        return weeks * 7;
    }
 
    static void Main()
    {
        // Set the number of weeks
        int weeks = 9;
 
        // Call the function to convert weeks to days
        int days = ConvertWeeksToDays(weeks);
 
        if (days == -1)
        {
            Console.WriteLine("Invalid Input");
        }
        else
        {
            // Display the result
            Console.WriteLine($"{weeks} weeks is equal to {days} days.");
        }
    }
}


Javascript




// Function to convert weeks to days
function convertWeeksToDays(weeks) {
    if (weeks < 0)
        return -1;
     
    // Convert weeks to days (1 week = 7 days)
    return weeks * 7;
}
 
// Main function
function main() {
    const weeks = 9;
 
    const days = convertWeeksToDays(weeks);
 
    if (days === -1) {
        console.log("Invalid Input");
    } else {
        // Display the result
        console.log(`${weeks} weeks is equal to ${days} days.`);
    }
}
 
// Execute the main function
main();


Output

9 weeks is equal to 63 days.






Time Complexity: O(1)
Auxiliary space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads