WAP in java to accept in time in second and print it in the form of hours minutes and seconds without scanner
Answers
class Time
{
public static void main(int t )
{
int hours = t*(1/3600);
t = t - (3600*hours);
int mins = t*(1/60);
t = t - (60*mins);
System.out.println("Hours "+hours);
System.out.println("Minutes "+mins);
System.out.println("Seconds "+ t);
}
}
-----> Pls compile it and go through the logic,if any query please ask in comments.Follow me for more Java related
doubts.✌️
Question:-
Write a program in Java to accept time in second and print it in the form of hours minutes and seconds without using scanner.
Code:-
import java.io.*;
class Time
{
public static void main(String args[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the time in seconds: ");
int sec=Integer.parseInt(br.readLine());
int hours=sec/3600;
sec-=hours*3600;
int min=sec/60;
sec-=min*60;
System.out.println("Hours: "+hours);
System.out.println("Minutes: "+min);
System.out.println("Seconds: "+sec);
}
}
Example:-
Enter the time in seconds: 3661
sec=3661
hours=3661/3600=1
sec-=hours*3600
Or,
sec-=3600
Or,
sec=61
min=61/60=1
sec-=min*60=1
Hence,
Hours: 1
Minutes: 1
Seconds: 1
I have used BufferedReader instead of Scanner class.