7. Write a program that converts total seconds (given by the user through a prompt) into days, hours, minutes, and seconds.
Example to understand the question:
You entered 500,000 seconds, it should output that 500,000 are same as 5 days, 18 hours, 53 minutes and 20 seconds.
Logic of the Program:
- Divide the input by 86400, if you get a number greater than 0, this is the number of days.
- Again, divide the remained number you get from the first calculation by 3600, this will give you the number of hours
- Divide the remainder of your second calculation by 60 which is the number of Minutes
- Finally the remained number from your third calculation is the number of seconds
import java.util.*; class seconds { public static void main(String [] arg) { Scanner sc = new Scanner(System.in); int sec; System.out.println("Enter Seconds"); sec=sc.nextInt(); int days = sec / 86400; int hours = (sec % 86400 ) / 3600; int minutes = ((sec % 86400 ) % 3600 ) / 60; int seconds = ((sec % 86400 ) % 3600 ) % 60; System.out.println("Total number of " + sec + " is converted to: " + days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds."); } }
Output:
0 Responses on Java Question Set I - Conditional Statements in Java|