Program to convert weeks to days
Last Updated :
12 Feb, 2024
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;
return weeks * 7;
}
int main()
{
int weeks = 9;
int days = convertWeeksToDays(weeks);
if (days == -1) {
cout << "Invalid Input" << endl;
}
else {
cout << weeks << " weeks is equal to " << days
<< " days." << endl;
}
return 0;
}
|
Java
public class WeekToDayConverter {
public static int convertWeeksToDays( int weeks) {
if (weeks < 0 ) {
return - 1 ;
}
return weeks * 7 ;
}
public static void main(String[] args) {
int weeks = 9 ;
int days = convertWeeksToDays(weeks);
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):
if weeks < 0 :
return - 1
return weeks * 7
if __name__ = = "__main__" :
weeks = 9
days = convert_weeks_to_days(weeks)
if days = = - 1 :
print ( "Invalid Input" )
else :
print (f "{weeks} weeks is equal to {days} days." )
|
C#
using System;
class Program
{
static int ConvertWeeksToDays( int weeks)
{
if (weeks < 0)
return -1;
return weeks * 7;
}
static void Main()
{
int weeks = 9;
int days = ConvertWeeksToDays(weeks);
if (days == -1)
{
Console.WriteLine( "Invalid Input" );
}
else
{
Console.WriteLine($ "{weeks} weeks is equal to {days} days." );
}
}
}
|
Javascript
function convertWeeksToDays(weeks) {
if (weeks < 0)
return -1;
return weeks * 7;
}
function main() {
const weeks = 9;
const days = convertWeeksToDays(weeks);
if (days === -1) {
console.log( "Invalid Input" );
} else {
console.log(`${weeks} weeks is equal to ${days} days.`);
}
}
main();
|
Output
9 weeks is equal to 63 days.
Time Complexity: O(1)
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...