1
+ /*
2
+ Write a program that asks the user to enter a number of seconds.
3
+
4
+ • There are 60 seconds in a minute.
5
+ If the number of seconds entered by the user is greater than or equal to 60,
6
+ the program should display the number of minutes in that many seconds.
7
+
8
+ • There are 3,600 seconds in an hour.
9
+ If the number of seconds entered by the user is greater than or equal to 3,600,
10
+ the program should display the number of hours in that many seconds.
11
+
12
+ • There are 86,400 seconds in a day.
13
+ If the number of seconds entered by the user is greater than or equal to 86,400,
14
+ the program should display the number of days in that many seconds.
15
+ */
16
+
17
+ package com .challenges ;
18
+
19
+ import java .util .Scanner ;
20
+
21
+ public class TimeCalculator {
22
+ public static void main (String [] args ) {
23
+
24
+ // Declare Variables
25
+ int noOfSecs ;
26
+ final int SECS_PER_MIN = 60 ;
27
+ final int SECS_PER_HOUR = 3600 ;
28
+ final int SECS_PER_DAY = 86400 ;
29
+ int days ;
30
+ int hours ;
31
+ int minutes ;
32
+
33
+ Scanner scanner = new Scanner (System .in );
34
+
35
+ System .out .println ("Please enter the number of seconds: " );
36
+ noOfSecs = scanner .nextInt ();
37
+
38
+ if (noOfSecs >= SECS_PER_DAY ) {
39
+
40
+ days = noOfSecs / SECS_PER_DAY ;
41
+ System .out .printf ("The number of days in %d seconds = %d\n " , noOfSecs , days );
42
+ noOfSecs %= SECS_PER_DAY ;
43
+
44
+ if (noOfSecs > SECS_PER_HOUR ) {
45
+
46
+ hours = noOfSecs / SECS_PER_HOUR ;
47
+ System .out .printf ("The number of hours in the remaining %d seconds = %d\n " , noOfSecs , hours );
48
+ noOfSecs %= SECS_PER_HOUR ;
49
+ }
50
+
51
+ if (noOfSecs > SECS_PER_MIN ) {
52
+
53
+ minutes = noOfSecs / SECS_PER_MIN ;
54
+ System .out .printf ("The number of minutes in the remaining %d seconds = %d\n " , noOfSecs , minutes );
55
+ noOfSecs %= SECS_PER_MIN ;
56
+ }
57
+
58
+ if (noOfSecs > 0 ) {
59
+
60
+ System .out .printf ("The number of remaining seconds = %d\n " , noOfSecs );
61
+ }
62
+
63
+ }
64
+ else if (noOfSecs >= SECS_PER_HOUR ) {
65
+
66
+ hours = noOfSecs / SECS_PER_HOUR ;
67
+ System .out .printf ("The number of hours in %d seconds = %d\n " , noOfSecs , hours );
68
+ noOfSecs %= SECS_PER_HOUR ;
69
+
70
+ if (noOfSecs > SECS_PER_MIN ) {
71
+
72
+ minutes = noOfSecs / SECS_PER_MIN ;
73
+ System .out .printf ("The number of minutes in the remaining %d seconds = %d\n " , noOfSecs , minutes );
74
+ noOfSecs %= SECS_PER_MIN ;
75
+ }
76
+
77
+ if (noOfSecs > 0 ) {
78
+
79
+ System .out .printf ("The number of remaining seconds = %d\n " , noOfSecs );
80
+ }
81
+ }
82
+ else if (noOfSecs >= SECS_PER_MIN ) {
83
+
84
+ minutes = noOfSecs / SECS_PER_MIN ;
85
+ System .out .printf ("The number of minutes in %d seconds = %d\n " , noOfSecs , minutes );
86
+ noOfSecs %= SECS_PER_MIN ;
87
+
88
+ if (noOfSecs > 0 ) {
89
+
90
+ System .out .printf ("The number of remaining seconds = %d\n " , noOfSecs );
91
+ }
92
+ }
93
+ else {
94
+ System .out .printf ("The number of seconds, %d, is less than even a minute." , noOfSecs );
95
+ }
96
+ }
97
+ }
0 commit comments