How to turn minutes into years and days?

Tags:



In the textbook it explains how to convert seconds into minutes with how many seconds remain but the question I have is as follows.

Write a program that prompts the user to enter the minutes (e.g., 1 billion), and displays the number of years and days for the minutes. For simplicity, assume a year has 365 days. Here is an example.

Enter the number of minutes: 100000000
100000000 minutes is approximately 1902 years and 214 days.

What I currently have is as follows:

import java.util.Scanner;

public class Ch2_Exercise2_7 {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    // Prompt user for number of minutes
    System.out.println("Enter the number of minutes:");
    int minutes = input.nextInt();

     // Number of minutes in a year
    int year = minutes / 525600;
    int day = minutes / 1440;
    int remainingMinutes = day % 525600;


    System.out.println(minutes + " minutes is " + year + " years and "  +  remainingMinutes + " days ");
    }

   }

With what I have it isn’t giving me the remaining minutes into days. For example, if I put in 525600 minutes it gives me 1 year and 365 days when it should just be 1 year.

I’m using Java Eclipse. Any help would be greatly appreciated! My apologies in advance if I post the code incorrectly.

Answer

You screwed up a bit over here:

// Number of minutes in a year
int year = minutes / 525600;
int day = minutes / 1440;
int remainingMinutes = day % 525600;

You took the total number of minutes and divided by 1440, so the number of days you got was wrong. You should have taken the remainder and then divided by 1440.

Another thing was in your print statement. You wrote the number of minutes left after one year as the number of days.

This should work:

// Number of minutes in a year
int year = minutes / 525600;
int remainingMinutes = minutes % 525600;
int day = remainingMinutes / 1440;

System.out.println(minutes + " minutes is approximately " + year + " years and " + day + " days.");


Source: stackoverflow