System.out.println("Extra credit\nRe-enter number of seconds:");          
  int j = input.nextInt();
  int i =(j/60);
  int k = (j%60);                         
  System.out.println(j+" seconds is " +i+" minutes and "+k+" seconds.");

Hi when i key in 30 for j the correct answer should be 30seconds is 0minutes and 0seconds.
However my above code gives me 30seconds is 0minutes and 30seconds.
What am i doing wrong?

Dani AI

Generated

As discovered, this is not a modulus bug. and are correct: with integer arithmetic the program gives whole minutes and the leftover seconds. For an input of 30 seconds there are 0 whole minutes and 30 leftover seconds, so the output you saw is the expected result.

Where confusion often comes from is the intended rounding rule. Three common behaviors and their results for 30 seconds:

  • keep whole minutes plus leftover seconds -> 0 minutes, 30 seconds
  • round to nearest minute -> 1 minute, 0 seconds
  • drop seconds entirely (floor to minutes) -> 0 minutes, 0 seconds

Choose the rule that matches the requirement and implement that conversion. For example, rounding-to-nearest uses a floating-point divide and round; rounding-up adds the right offset before integer division; dropping seconds simply prints zero for seconds. Also remember edge cases: 60 seconds should produce 1 minute 0 seconds, 90 gives 1 minute 30 seconds, and negative inputs need explicit handling because Java remainders preserve sign.

Practical tips: verify the input unit (seconds versus milliseconds), print intermediate values while debugging, and use formatted output for readability (for example, String.format("%d:%02d", minutes, seconds) or String.format("%d minutes and %02d seconds", minutes, seconds)) so seconds always show two digits.

Recommended Answers

All 2 Replies

Member Avatar for Member #887084

Wouldn't the correct answer be "30seconds is 0minutes and 30seconds.", in which case your code is working perfect.

Perhaps you can clarify why 30 != 30?

It's not really a bug, the modulus is doing whats its suppose to do

are you trying to make the program display 0 when 30 or a similar case is entered?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.