Find AM or PM JAVA AVG PART 003
Find AM or PM
A string S which represents the time in 24 hour format HH:MM is passed as input. The program must find if it is AM or PM and print it as output.
If an invalid time is passed as input, the program must print INVALIDINPUT
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String[] c=s.split(":");
int a=Integer.parseInt(c[0]);
int b=Integer.parseInt(c[1]);
if(a>=0 && a<12)
System.out.print("AM");
else if(a>=12 && a<=23)
System.out.print("PM");
else
System.out.print("INVALIDINPUT");
}
}
Comments
Post a Comment