Program to convert given weeks to hours
Last Updated :
20 Feb, 2024
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: 2520
Input: 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:
- Use a function DaysToWeek() to convert the days to equivalent number of hours using the formula:
Below is the implementation of the algorithm:
C++
#include <bits/stdc++.h>
using namespace std;
int WeekToDay( int weeks)
{
int days = weeks * 7;
return days;
}
int DayToHour( int days)
{
int hours = days * 24;
return hours;
}
int main()
{
int weeks = 15;
int days = WeekToDay(weeks);
int hours = DayToHour(days);
cout << hours << endl;
weeks = 1;
days = WeekToDay(weeks);
hours = DayToHour(days);
cout << hours << endl;
}
|
Java
public class TimeConversion {
static int weekToDay( int weeks) {
int days = weeks * 7 ;
return days;
}
static int dayToHour( int days) {
int hours = days * 24 ;
return hours;
}
public static void main(String[] args) {
int weeks = 15 ;
int days = weekToDay(weeks);
int hours = dayToHour(days);
System.out.println(hours);
weeks = 1 ;
days = weekToDay(weeks);
hours = dayToHour(days);
System.out.println(hours);
}
}
|
Python3
def week_to_day(weeks):
days = weeks * 7
return days
def day_to_hour(days):
hours = days * 24
return hours
if __name__ = = "__main__" :
weeks = 15
days = week_to_day(weeks)
hours = day_to_hour(days)
print (hours)
weeks = 1
days = week_to_day(weeks)
hours = day_to_hour(days)
print (hours)
|
C#
using System;
class Program
{
static int WeekToDay( int weeks)
{
int days = weeks * 7;
return days;
}
static int DayToHour( int days)
{
int hours = days * 24;
return hours;
}
static void Main()
{
int weeks = 15;
int days = WeekToDay(weeks);
int hours = DayToHour(days);
Console.WriteLine(hours);
weeks = 1;
days = WeekToDay(weeks);
hours = DayToHour(days);
Console.WriteLine(hours);
}
}
|
Javascript
function weekToDay(weeks) {
var days = weeks * 7;
return days;
}
function dayToHour(days) {
var hours = days * 24;
return hours;
}
var weeks = 15;
var days = weekToDay(weeks);
var hours = dayToHour(days);
console.log(hours);
weeks = 1;
days = weekToDay(weeks);
hours = dayToHour(days);
console.log(hours);
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...