Thursday, June 19, 2014

Elementary Programming; Exercise No. 7

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.

Solution:

public static void main(String[] args) {
  Scanner s = new Scanner(System.in);
  int minutes, years, days, minutesPerDay, minutesPerYear;

  System.out.print("Enter the number of minutes:");
  minutes = s.nextInt();

  minutesPerDay = 60 * 24;
  minutesPerYear = minutesPerDay * 365;

  years = minutes / minutesPerYear; // get the number of years by dividing total minutes with minutes per year
  days = (minutes / minutesPerDay) % 365; // get the number of days

  System.out.print(minutes + " is aproximately " + years + " years and " + days + ".");
}

No comments:

Post a Comment

Author