Write a program to convert seconds to hour, minute and seconds


This is a Java program that converts a given number of seconds to hours, minutes, and seconds. Here's a breakdown of how the program works:

  • The program first imports the Scanner class from the java.util package, which allows the user to input data from the keyboard.
  • The program then defines a class called Convert_Second.
  • Inside the class, the main method is defined, which is the entry point of the program.
  • The Scanner object is created to read input from the user.
  • The program prompts the user to enter the number of seconds to be converted.
  • The user input is read and stored in the variable 'seconds'.
  • The program then calculates the number of seconds left after converting the original number of seconds to minutes.
  • The program calculates the number of minutes left after converting the original number of seconds to hours.
  • The program calculates the number of hours by dividing the total number of minutes by 60.
  • The program prints the result in the format "hours:minutes:seconds" using the System.out.println() method.

Overall, this program demonstrates how to perform basic arithmetic operations in Java and how to use the Scanner class to read user input.

Source Code

import java.util.Scanner;
class Convert_Second
 {
	public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the seconds: ");
	    int seconds = in.nextInt();
        int sec = seconds % 60;
        int hour = seconds / 60;
        int min = hour % 60;
        hour = hour / 60;
        System.out.println( hour + ":" + min + ":" + sec);
    }
 }
 

Output

Enter the seconds: 5240
1:27:20