Advertisement
Salman_CUET_18

Time conversion

Mar 25th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5. char time[10];
  6.  
  7. int i, frst_dgit, scnd_dgit, temp;
  8.  
  9. scanf("%s", time);
  10.  
  11. if(time[8] == 'P' && time[9] == 'M')
  12. {
  13. if(time[0] == '1' && time[1] == '2')
  14. {
  15. for(i = 0; i < 8; i++) printf("%c", time[i]);
  16.  
  17. ///in 24 hour format we don't need last two character PM or AM, so conduct the loop until 7th index
  18. }
  19. else
  20. {
  21. scnd_dgit = time[1] - '0';
  22. //convert the character into integer, first two character indicates hour and that the things we have to convert into 24 hour format
  23. frst_dgit = time[0] - '0';
  24. //we have to add 12 with given hour, so we will add 1 with first digit of hour and 2 with second digit of hour
  25. if(scnd_dgit + 2 >= 10)
  26. {
  27. //if any value of second digit stands greater than 10 after adding two , we have to subtract 10 and add one with first digit
  28. time[1] = (scnd_dgit + 2 - 10)+ '0';
  29.  
  30. ///as greater than 10 , we subtract it from second digit and add one with first digit
  31.  
  32. time[0] =(frst_dgit + 1 + 1)+ '0';
  33.  
  34. for(i = 0; i < 8; i++) printf("%c", time[i]);
  35. }
  36. else
  37. {
  38. time[1] = (scnd_dgit + 2) + '0';
  39.  
  40. time[0] = (frst_dgit + 1) + '0';
  41.  
  42. for(i = 0; i < 8; i++) printf("%c", time[i]);
  43. }
  44. }
  45. }
  46.  
  47. else if(time[8] == 'A' && time[9] == 'M')
  48. {
  49. if(time[0] == '1' && time[1] == '2')
  50. {
  51. time[0]=time[1]='0';
  52. //according to the condition in the question if 12:00:00AM then in 24 hour format it will be 00:00:00, so we made first two character zero
  53. for(i = 0; i < 8; i++) printf("%c", time[i]);
  54. }
  55. else
  56. for(i = 0; i < 8; i++) printf("%c", time[i]);
  57. }
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement