Calculate the angle between hour hand and minute hand

14 Jan, 2022
This problem is known as Clock angle problem where we need to find angle between hands of an analog clock at a given time.Examples: 
Input:  
h = 12:00
m = 30.00
Output: 
165 degree

Input:  
h = 3.00
m = 30.00
Output: 
75 degree
The idea is to take 12:00 (h = 12, m = 0) as a reference. Following are detailed steps.
1. Calculate the angle made by hour hand with respect to 12:00 in h hours and m minutes. 2. Calculate the angle made by minute hand with respect to 12:00 in h hours and m minutes. 3. The difference between the two angles is the angle between the two hands.
How to calculate the two angles with respect to 12:00? The minute hand moves 360 degrees in 60 minute(or 6 degrees in one minute) and hour hand moves 360 degrees in 12 hours(or 0.5 degrees in 1 minute). In h hours and m minutes, the minute hand would move (h*60 + m)*6 and hour hand would move (h*60 + m)*0.5.  
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program to find angle between hour and minute hands#include <bits/stdc++.h>using namespace std; // Utility function to find minimum of two integersint min(int x, int y){    return (x < y)? x: y;     } int calcAngle(double h, double m){    // validate the input    if (h <0 || m < 0 || h >12 || m > 60)        printf("Wrong input");     if (h == 12) h = 0;    if (m == 60)     {      m = 0;      h += 1;       if(h>12)        h = h-12;     }     // Calculate the angles moved    // by hour and minute hands    // with reference to 12:00    float hour_angle = 0.5 * (h * 60 + m);    float minute_angle = 6 * m;     // Find the difference between two angles    float angle = abs(hour_angle - minute_angle);     // Return the smaller angle of two possible angles    angle = min(360 - angle, angle);     return angle;} // Driver Codeint main(){    cout << calcAngle(9, 60) << endl;    cout << calcAngle(3, 30) << endl;    return 0;} // This is code is contributed by rathbhupendra
// C program to find angle between hour and minute hands#include <stdio.h>#include <stdlib.h> // Utility function to find minimum of two integersint min(int x, int y) { return (x < y)? x: y; } int calcAngle(double h, double m){    // validate the input    if (h <0 || m < 0 || h >12 || m > 60)        printf("Wrong input");     if (h == 12) h = 0;    if (m == 60)     {      m = 0;      h += 1;       if(h>12)        h = h-12;     }      // Calculate the angles moved by hour and minute hands    // with reference to 12:00    int hour_angle = 0.5 * (h*60 + m);    int minute_angle = 6*m;     // Find the difference between two angles    int angle = abs(hour_angle - minute_angle);     // Return the smaller angle of two possible angles    angle = min(360-angle, angle);     return angle;} // Driver Codeint main(){    printf("%d n", calcAngle(9, 60));    printf("%d n", calcAngle(3, 30));    return 0;}
// Java program to find angle between hour and minute handsimport java.io.*; class GFG{    // Function to calculate the angle    static int calcAngle(double h, double m)    {        // validate the input        if (h <0 || m < 0 || h >12 || m > 60)            System.out.println("Wrong input");         if (h == 12)            h = 0;             if (m == 60)       {        m = 0;        h += 1;        if(h>12)          h = h-12;        }          // Calculate the angles moved by hour and minute hands        // with reference to 12:00        int hour_angle = (int)(0.5 * (h*60 + m));        int minute_angle = (int)(6*m);         // Find the difference between two angles        int angle = Math.abs(hour_angle - minute_angle);         // smaller angle of two possible angles        angle = Math.min(360-angle, angle);         return angle;    }         // Driver Code    public static void main (String[] args)    {        System.out.println(calcAngle(9, 60)+" degree");        System.out.println(calcAngle(3, 30)+" degree");    }} // Contributed by Pramod Kumar
# Python program to find angle# between hour and minute hands # Function to Calculate angle b/w# hour hand and minute handdef calcAngle(h,m):                 # validate the input        if (h < 0 or m < 0 or h > 12 or m > 60):            print('Wrong input')                 if (h == 12):            h = 0        if (m == 60):            m = 0            h += 1;            if(h>12):                   h = h-12;                 # Calculate the angles moved by        # hour and minute hands with        # reference to 12:00        hour_angle = 0.5 * (h * 60 + m)        minute_angle = 6 * m                 # Find the difference between two angles        angle = abs(hour_angle - minute_angle)                 # Return the smaller angle of two        # possible angles        angle = min(360 - angle, angle)                 return angle # Driver Codeh = 9m = 60print('Angle ', calcAngle(h,m)) # This code is contributed by Danish Raza
// C# program to find angle between// hour and minute handsusing System; class GFG {         // Function to calculate the angle    static int calcAngle(double h, double m)    {        // validate the input        if (h < 0 || m < 0 ||            h > 12 || m > 60)            Console.Write("Wrong input");         if (h == 12)            h = 0;                    if (m == 60)       {        m = 0;        h += 1;        if(h>12)          h = h-12;       }         // Calculate the angles moved by hour and        // minute hands with reference to 12:00        int hour_angle = (int)(0.5 * (h * 60 + m));        int minute_angle = (int)(6 * m);         // Find the difference between two angles        int angle = Math.Abs(hour_angle - minute_angle);         // smaller angle of two possible angles        angle = Math.Min(360 - angle, angle);         return angle;    }         // Driver code    public static void Main ()    {        Console.WriteLine(calcAngle(9, 60));        Console.Write(calcAngle(3, 30));    }} // This code is contributed by Nitin Mittal.
<?php// PHP program to find// angle between hour// and minute hands // Utility function to// find minimum of two// integersfunction mintwo($x, $y){    return ($x < $y) ?                  $x : $y;} function calcAngle($h, $m){    // validate the input    if ($h <0 || $m < 0 ||        $h >12 || $m > 60)        echo "Wrong input";     if ($h == 12) $h = 0;    if ($m == 60) {        $m = 0;        $h += 1;        if($h>12)          $h = $h-12;    }     // Calculate the angles    // moved by hour and    // minute hands with    // reference to 12:00    $hour_angle = 0.5 *                  ($h * 60 + $m);    $minute_angle = 6 * $m;     // Find the difference    // between two angles    $angle = abs($hour_angle -                 $minute_angle);     // Return the smaller angle    // of two possible angles    $angle = min(360 - $angle,                       $angle);         return $angle;} // Driver Codeecho calcAngle(9, 60), "\n";echo calcAngle(3, 30), "\n"; // This code is contributed by ajit?>
<script>// Javascript program to find angle between hour and minute hands // Utility function to find minimum of two integersfunction min(x, y){    return (x < y)? x: y;     } function calcAngle(h, m){    // validate the input    if (h <0 || m < 0 || h >12 || m > 60)        document.write("Wrong input");     if (h == 12) h = 0;    if (m == 60)    {    m = 0;    h += 1;    if(h>12)        h = h-12;    }     // Calculate the angles moved    // by hour and minute hands    // with reference to 12:00    let hour_angle = 0.5 * (h * 60 + m);    let minute_angle = 6 * m;     // Find the difference between two angles    let angle = Math.abs(hour_angle - minute_angle);     // Return the smaller angle of two possible angles    angle = min(360 - angle, angle);     return angle;} // Driver Code    document.write(calcAngle(9, 60) + "<br>");    document.write(calcAngle(3, 30) + "<br>");     // This code is contributed by Surbhi Tyagi. </script>
60
75
Time Complexity: O(1)
Auxiliary Space: O(1)Exercise: Find all times when hour and minute hands get superimposed.This article is contributed by Ashish Bansal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 
nitin mittal
jit_t
rathbhupendra
kunalyadav2
pranshul2112
tejendrapratap
surbhityagi15
subhammahato348
amartyaghoshgfg
Amazon
Infinera
Paytm
Salesforce
Mathematical
Paytm
Amazon
Salesforce
Infinera
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Program to find GCD or HCF of two numbers
Minimum number of jumps to reach end
Find minimum number of coins that make a given value
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Program for Decimal to Binary Conversion
Modulo 10^9+7 (1000000007)