Write a program in Java to input the number of days and display it after converting into a number of years, months and days
Answers
Answer:
import java.util.Scanner;
public class KboatDayConversion
{
public static void main(String args[])
{
//Declaration
Scanner in = new Scanner(System.in);
int days, years, months, d;
//Prompt and accept the number of days from user
System.out.print("Enter days: ");
int days = in.nextInt();
//Computation
years = days / 365;
days = days - (365 * years);
months = days / 30;
d = days - (months * 30);
//Display
System.out.println(years + " Years " + " " + months + " " + " Months "
+ " " + d + " + " " + Days");
}
}
Answer:
import java.util.Scanner;
public class DaysConverter {
public static void main (String [] args) {
Scanner sc = new Scanner(System.in);
//Taking input from the user
System.out.println("Here, a month is considered to have 30 day");
System.out.print("Enter days: ");
int days = sc.nextInt();
//Most important part comes here
int years = days/365;
int months = (days % 365) / 30;
int Days = (days % 365) % 30;
//Printing the output
System.out.println("Year/s : " + years);
System.out.println("Month/s : " + months);
System.out.println("Day/s : " + Days);
}
}
Output:
Here, a month is considered to have 30 day
Enter days: 1234
Year/s : 3
Month/s : 4
Day/s : 19