-4

I'm assigned with creating the inverse of a program I wrote a program that gave me the total number of seconds when I put in the hour minutes and seconds (ex) 1hr28m42s=5322seconds -now I need to make it so that when i put in the seconds it tells me the time, (ex)9999seconds=2h46m39s

I've tried to google the inverse of numbers, but alot of results just come up with the inverse of points or matrixes, i tried searching on stackoverflow with the tag inverse but i dont know if im doing it right if you guys have seen this question asked already im sorry and please redirect me!

So far i only assigned variables for second coversion and not minutes and hours but even that is no good, i could post my other code for my first ask converting time into seconds if anything

How do I cap off an integer at a certain point so that it resets after? (example) if x goes over 60 it goes back to 1? thanks guys

code so far:

//Assigned variables to distinguish time
int second = 1;
int minute = second + 59;
int hour = minute * 60;

int x; // x tells you the time

//Enters the seconds for conversion
System.out.println ("Enter the seconds to be converted: ");
second = scan.nextInt();

//This print tells you the above information in terms 
//of total seconds capping off at 60
x = second + (second /60) + (second/60/60);
System.out.println ("The total time in seconds is " +x);
Nitesh
  • 2,286
  • 2
  • 43
  • 65

1 Answers1

2

You want to approach it differently (at least using most programming languages).

You already know that going from 2 hours, 12 minutes and 5 seconds you take the number of seconds in 2 hours, add the number of seconds in 12 minutes and then add the last 5 seconds.

For the other way around you do it this way.

You start with 7925 seconds.

  1. Check how many whole hours fits in this interval (2 hours).
  2. Calculate how many seconds remains (725).
  3. From the remaining seconds, check how many whole minutes fit in this interval (12).
  4. Calculate the number of remaining seconds (5).

Now you are done and have 2 hours, 12 minutes and 5 seconds.

Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53