Monday, August 20, 2018

TIME DIFFERENCE

  • Problem Description

    Help Rama to achieve his friends task "Time challenge" to display hours minutes and seconds in both 12 and 24 hours format: 24 Hours format : 23:30:12 Standard format : 11:30:12 pm

    Refer the sample input and output:

    Sample Input 1:
    20 25 06

    Output 1:
    24 Hours Format
    20:25:06
    12 Hours Format
    08:25:06 pm

    Sample Input 2:
    24 25 06

    Output 2:
    Invalid Time

    Sample Input 3:
    23 60 06

    Output 3:
    Invalid Time

    Sample Input 4:
    23 56 61

    Output 4:
    Invalid Time
  • CODING ARENA::
  • #include <stdio.h>
    struct time
    {
      int h,m,s;
    }a;
    int main()
    {
      scanf("%d %d %d",&a.h,&a.m,&a.s);
      if(a.h>=24 && a.m>1 &&a.s>2)
      {
        printf("Invalid Time");
    }
    else if (a.m>60 || a.s>60)
    {
    if(a.s>60 || a.m>60)
    {
      printf("Invalid Time");
    }
    }
    else if (a.h<=12)
    {
      printf("24 Hours Format");
      printf("\n%d:%d:%d",a.h,a.m,a.s);
      printf("\n12 Hours Format");
      printf("\n%d:%d:%d am",a.h,a.m,a.s);
    }
    else 
    {
      printf("24 Hours Format");
      printf("\n%d:%d:%d",a.h,a.m,a.s);
      printf("\n12 Hours Format");
      printf("\n%d:%d:%d pm",a.h-12,a.m,a.s);
    }
    return 0;
    }
  • Test Case 1

    Input (stdin)
    23
    
    35
    
    22
    
    
    Expected Output
    24 Hours Format
    
    23:35:22
    
    12 Hours Format
    
    11:35:22 pm
  • Test Case 2

    Input (stdin)
    24
    
    56
    
    59
    
    
    Expected Output
    Invalid Time

No comments:

Post a Comment