In this article, we will learn to write Kotlin program to Convert a Given Number of Days in terms of Years, Weeks & Days.

Source Code:

import java.util.Scanner

fun main(args: Array<String>) {

var totalDays: Int

val years: Int

val weeks: Int

val days: Int

val s = Scanner(System.`in`)

print("Enter the number of days:")

totalDays = s.nextInt()

years = totalDays / 365

weeks = (totalDays % 365) / 7

days = (totalDays % 365) % 7

println("No. of years:$years")

println("No. of weeks:$weeks")

println("No. of days:$days")

}

fun main(args: Array<String>) {

var totalDays: Int

val years: Int

val weeks: Int

val days: Int

val s = Scanner(System.`in`)

print("Enter the number of days:")

totalDays = s.nextInt()

years = totalDays / 365

weeks = (totalDays % 365) / 7

days = (totalDays % 365) % 7

println("No. of years:$years")

println("No. of weeks:$weeks")

println("No. of days:$days")

}

Output:

Enter the number of days:366

No. of years:1

No. of weeks:0

No. of days:1

No. of years:1

No. of weeks:0

No. of days:1

Description:

In the above program, we have taken input from a user to enter the number of days as an Integer input. Next, we have used an equation "years = totalDays / 365" to find the number of years from the total number of days entered.

To find the total number of weeks, we have used an equation "weeks = (totalDays % 365) / 7". And to find days, an equation is "days = (totalDays % 365) % 7".

## 0 Comments