Program to convert given weeks to hours
Given a positive integer W which represents the number of weeks, your task is to convert it to the total number of hours.
Examples:
Input: W=15
Output: 2520Input: W=1
Output: 168
Approach: To solve the problem, follow the below idea:
The problem can be solved using simple mathematical conversions:
- 1 Week = 7 days
- 1 Day = 24 hours
Step-by-step algorithm:
- Create a function WeekToDays() that converts the number of weeks to equivalent number of days using the formula:
- 1 Week = 7 days
- Use a function DaysToWeek() to convert the days to equivalent number of hours using the formula:
- 1 Day = 24 hours
Below is the implementation of the algorithm:
C++
#include <bits/stdc++.h> using namespace std; // funtion to convert week to days int WeekToDay( int weeks) { int days = weeks * 7; return days; } // funtion to convert Days to hours int DayToHour( int days) { int hours = days * 24; return hours; } // driver code int main() { int weeks = 15; // get total number of days from week int days = WeekToDay(weeks); // get total number of hours from days int hours = DayToHour(days); cout << hours << endl; weeks = 1; // get total number of days from week days = WeekToDay(weeks); // get total number of hours from days hours = DayToHour(days); cout << hours << endl; } |
Java
public class TimeConversion { // Function to convert weeks to days static int weekToDay( int weeks) { int days = weeks * 7 ; return days; } // Function to convert days to hours static int dayToHour( int days) { int hours = days * 24 ; return hours; } // Driver code public static void main(String[] args) { // Given weeks int weeks = 15 ; // Get total number of days from weeks int days = weekToDay(weeks); // Get total number of hours from days int hours = dayToHour(days); // Print the result System.out.println(hours); // Update weeks weeks = 1 ; // Get total number of days from weeks days = weekToDay(weeks); // Get total number of hours from days hours = dayToHour(days); // Print the result System.out.println(hours); } } |
Python3
# Function to convert weeks to days def week_to_day(weeks): days = weeks * 7 return days # Function to convert days to hours def day_to_hour(days): hours = days * 24 return hours # Driver code if __name__ = = "__main__" : # Given weeks weeks = 15 # Get total number of days from weeks days = week_to_day(weeks) # Get total number of hours from days hours = day_to_hour(days) # Print the result print (hours) # Update weeks weeks = 1 # Get total number of days from weeks days = week_to_day(weeks) # Get total number of hours from days hours = day_to_hour(days) # Print the result print (hours) |
C#
using System; class Program { // Function to convert weeks to days static int WeekToDay( int weeks) { int days = weeks * 7; return days; } // Function to convert days to hours static int DayToHour( int days) { int hours = days * 24; return hours; } // Main function static void Main() { int weeks = 15; // Get the total number of days from weeks int days = WeekToDay(weeks); // Get the total number of hours from days int hours = DayToHour(days); Console.WriteLine(hours); weeks = 1; // Get the total number of days from weeks days = WeekToDay(weeks); // Get the total number of hours from days hours = DayToHour(days); Console.WriteLine(hours); } } |
Javascript
// Function to convert weeks to days function weekToDay(weeks) { var days = weeks * 7; return days; } // Function to convert days to hours function dayToHour(days) { var hours = days * 24; return hours; } // Driver code // Given weeks var weeks = 15; // Get total number of days from weeks var days = weekToDay(weeks); // Get total number of hours from days var hours = dayToHour(days); // Print the result console.log(hours); // Update weeks weeks = 1; // Get total number of days from weeks days = weekToDay(weeks); // Get total number of hours from days hours = dayToHour(days); // Print the result console.log(hours); |
Output
2520 168
Time Complexity: O(1)
Auxiliary Space: O(1)