-
java seconds program
hi its me again
i have another program i am doing and wounderd if somebody could check if this answer is ok.
and also could somebody explain how i would do an analysis of this problem and also the calculations to show expected results for 3793 seconds.
Write a program to prompt the user for a positive integer number of seconds and output the number of hours, minutes and seconds that it represents. Test your program with 3793 seconds.
code
import java.util.Scanner;
public class Seconds
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Input number of seconds: ");
int seconds = scanner.nextInt();
int hours = seconds / 3600;
seconds = seconds % 3600;
int minutes = seconds / 60;
seconds = seconds % 60;
System.out.println("hours = "+hours);
System.out.println("minutes = "+minutes);
System.out.println("seconds = "+seconds);
}
}
results
H:\FPT>java Seconds
Input number of seconds: 3793
hours = 1
minutes = 3
seconds = 13
many thanks Andy..
lts
-
Re: java seconds program
Is your answer correct when you check it with a calculator?
-
Re: java seconds program
hi not sure how to work this out on a calculator i kind of get how it works
user enters 7680 secs.
(1 hr / 3600 secs) * (7680 secs) = 2 hrs
7680 - (3600 * 2) = 480 secs leftover
To get the leftover you could also do 7680%3600 = 480
(1 min / 60 secs) * (480 secs) = 8 mins
but to put this in a calculator ?