-4

I have written a program to change seconds value into seconds, minutes, hours, and Days. All other values are giving exact output except Hours. Whenever a user gives more than 86400 value it gives more than 24 hours value which I don't want. ..

int days = totalSeconds / 86400;
totalSeconds -= days * 86400;
int hours = totalSeconds % 3600;
totalSeconds -= hours * 3600;
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;
Faizan Ahmad
  • 355
  • 3
  • 14

1 Answers1

1

If you start with totalSeconds as an int to be broken down into seconds, minutes, hours, and days, this sequence should work with hours maxing out at 23:

int days = totalSeconds / 86400;
totalSeconds -= days * 86400;
int hours = totalSeconds / 3600;
totalSeconds -= hours * 3600;
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;
kshetline
  • 12,547
  • 4
  • 37
  • 73