-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeCalc2.java
More file actions
37 lines (31 loc) · 1.15 KB
/
TimeCalc2.java
File metadata and controls
37 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/***
Time Calculation II
Write a program (TimeCalc2) that reads an integer representing a time duration in seconds,
and prints the equivalent time duration in hours, minutes, and seconds.
Here is a run-time example:
Enter time duration in seconds: 8922
This is 2 hours, 28 minutes, and 42 seconds.
Here is another run-time example:
Enter time duration in seconds: 2017
This is 0 hours, 33 minutes, and 37 seconds.
(As usual, the user's input is underlined; everything else is generated by the program).
Assume that the entered value is a non-negative integer.
*/
import java.util.Scanner;
public class TimeCalc2 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Get the user's input
System.out.print("Enter time duration in seconds: ");
int timeInSeconds = scan.nextInt();
//Calculate hours
int hour = timeInSeconds / 3600;
//Calculate minutes
int minutes = (timeInSeconds - hour * 3600) / 60;
//Calculate seconds
int seconds = timeInSeconds - hour * 3600 - minutes * 60;
//Print output
System.out.println("This is " + hour + " hours, " + minutes + ", and " + seconds + " seconds.");
scan.close();
}
}