Solutions for Class 10 ICSE Logix Kips Computer Applications with BlueJ Java | IT Developer <?php echo $page_title; ?>
IT Developer

Input in Java

Chapter 6

Input in Java

Class 10 - Logix Kips ICSE Computer Applications with BlueJ


Share with a Friend

Java Program: Convert Seconds to Hours, Minutes & Seconds


Write a program in Java that accepts the seconds as input and converts them into the corresponding number of hours, minutes and seconds. A sample output is shown below:

Enter Total Seconds:
5000
1 Hour(s) 23 Minute(s) 20 Second(s)

import java.util.Scanner;

 

public class TimeConverter {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        // Input total seconds

        System.out.print("Enter Total Seconds: ");

        int totalSeconds = sc.nextInt();

 

        // Conversion logic

        int hours = totalSeconds / 3600;

        int remainingSeconds = totalSeconds % 3600;

        int minutes = remainingSeconds / 60;

        int seconds = remainingSeconds % 60;

 

        // Output

        System.out.println(hours + " Hour(s) " +

                           minutes + " Minute(s) " +

                           seconds + " Second(s)");

 

        sc.close();

    }

}

Output

 
SAMPLE INPUT : 
Enter Total Seconds:
5000

SAMPLE OUTPUT : 
1 Hour(s) 23 Minute(s) 20 Second(s)

Explanation

  • 1 hour = 3600 seconds
  • 1 minute = 60 seconds

Step-by-step calculation:

1. Hours

5000 ÷ 3600 = 1 hour  

2. Remaining seconds

5000 mod 3600 = 1400   

3. Minutes

1400 ÷ 60 = 23 minutes   

4. Seconds

1400 mod 60 = 20 seconds