I need to use the modulo operand to convert an inputted number of days to its equivalent number of year/s, month/s, week/s, and days. We just started discussion yesterday so I'm still a bit rusty but this is the program I made:
import java.util.Scanner;
public class DaysConvert {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int noOfDays, year, month, week, days;
System.out.print("Enter Number of Days: ");
noOfDays = input.nextInt();
year = noOfDays/365;
month = year%365;
week = month%30;
days = week%7;
System.out.println("Year: " + year);
System.out.println("Month: " + month);
System.out.println("Week: " + week);
System.out.println("Day: " + days);
}
}
Is my algorithm wrong or something?
Do it like this:
If you are just going to a fixed
30 days
in a month365 days
in a year with no leap year then you can iterate to the number of days in a for-loop and check the modulo of each one from the the user's inputted.sample:
result:
This will work properly:
i hope this helps.