1. Member
Join Date
Sep 2011
Posts
69
Rep Power
0

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

2. Member
Join Date
Sep 2011
Posts
69
Rep Power
0

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 ?

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•