Program to convert days to weeks
Write a program to convert a given number of days to weeks.
Examples:
Input: 14 days
Output: 2 weeksInput: 30 days
Output: 4 weeks and 2 days
Approach: To solve the problem, follow the below idea:
Divide the given number of days by 7 to get the number of weeks. The remainder will be the remaining days.
Step-by-step algorithm:
- Read the number of days.
- Calculate the number of weeks and remaining days.
- Display the result.
Below is the implementation of the algorithm:
C++
#include <iostream> using namespace std; int main() { int totalDays = 30; int weeks = totalDays / 7; int remainingDays = totalDays % 7; cout << totalDays << " days is equal to " << weeks << " weeks and " << remainingDays << " days." << endl; return 0; } |
C
#include <stdio.h> int main() { int totalDays = 30; int weeks = totalDays / 7; int remainingDays = totalDays % 7; printf ( "%d days is equal to %d weeks and %d days.\n" , totalDays, weeks, remainingDays); return 0; } |
Java
public class DaysToWeeksConverter { public static void main(String[] args) { int totalDays = 30 ; int weeks = totalDays / 7 ; int remainingDays = totalDays % 7 ; System.out.println(totalDays + " days is equal to " + weeks + " weeks and " + remainingDays + " days." ); } } |
Python3
total_days = 30 weeks = total_days / / 7 remaining_days = total_days % 7 print (f "{total_days} days is equal to {weeks} weeks and {remaining_days} days." ) |
C#
using System; class Program { static void Main() { int totalDays = 30; int weeks = totalDays / 7; int remainingDays = totalDays % 7; Console.WriteLine( $ "{totalDays} days is equal to {weeks} weeks and {remainingDays} days." ); } } |
Javascript
let totalDays = 30; let weeks = Math.floor(totalDays / 7); let remainingDays = totalDays % 7; console.log(`${totalDays} days is equal to ${weeks} weeks and ${remainingDays} days.`); |
Output
30 days is equal to 4 weeks and 2 days.
Time Complexity: O(1)
Auxiliary Space: O(1)