Computer Science, asked by pihu16, 1 year ago

Write a program in Java to accept no. of days and display the result after converting it into no. of years, no.of months and the remaining no. of days.

Answers

Answered by pundirmayank
4
  let assume you put the no of days is 100

int days=100;
int year= days/365;
int remaining_days=days%365;
int month=0;
if(remaining_days!=0){
month=remaining_days/30;
remaining_days=remaining_days%30;
}
System.out.print("Year="+year+", month="+month+", days="+remaining_days);

pundirmayank: After running this program answer will be
pundirmayank: Year=0, month=3, days=10
pihu16: U r not counting the leap year!! there are some years which are divisible by 4 but are not leap years. wt condition should i give for that.
pundirmayank: I think there is no need to show the leap year according to you question. But for finding out the Leap Year , You will modulus the year by 4. If Modulus result is 0 for that year. That year will be Leap year. Here is the condition, ( year%4 == 0 )
Answered by Shivu516
0

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 days

Enter days: 1234

Year/s : 3

Month/s : 4

Day/s : 19

Similar questions