fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // Utility function to find minimum of two integers
  5. int min(int x, int y) { return (x < y)? x: y; }
  6.  
  7. int calcAngle(double h, double m)
  8. {
  9. // validate the input
  10. if (h <0 || m < 0 || h >12 || m > 60)
  11. printf("Wrong input");
  12.  
  13. if (h == 12) h = 0;
  14. if (m == 60) m = 0;
  15.  
  16. // Calculate the angles moved by hour and minute hands
  17. // with reference to 12:00
  18. int hour_angle = 0.5 * (h*60 + m);
  19. int minute_angle = 6*m;
  20.  
  21. // Find the difference between two angles
  22. int angle = abs(hour_angle - minute_angle);
  23.  
  24. // Return the smaller angle of two possible angles
  25. angle = min(360-angle, angle);
  26.  
  27. return angle;
  28. }
  29.  
  30. // Driver program to test above function
  31. int main()
  32. {
  33. printf("%d \n", calcAngle(1, 2));
  34. printf("%d \n", calcAngle(3, 30));
  35. return 0;
  36. }
Success #stdin #stdout 0s 2156KB
stdin
Standard input is empty
stdout
19 
75