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++
#include <bits/stdc++.h>
using namespace std;
int min( int x, int y)
{
return (x < y)? x: y;
}
int calcAngle( double h, double m)
{
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;
}
float hour_angle = 0.5 * (h * 60 + m);
float minute_angle = 6 * m;
float angle = abs (hour_angle - minute_angle);
angle = min(360 - angle, angle);
return angle;
}
int main()
{
cout << calcAngle(9, 60) << endl;
cout << calcAngle(3, 30) << endl;
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
int min( int x, int y) { return (x < y)? x: y; }
int calcAngle( double h, double m)
{
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;
}
int hour_angle = 0.5 * (h*60 + m);
int minute_angle = 6*m;
int angle = abs (hour_angle - minute_angle);
angle = min(360-angle, angle);
return angle;
}
int main()
{
printf ( "%d n" , calcAngle(9, 60));
printf ( "%d n" , calcAngle(3, 30));
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int calcAngle( double h, double m)
{
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 ;
}
int hour_angle = ( int )( 0.5 * (h* 60 + m));
int minute_angle = ( int )( 6 *m);
int angle = Math.abs(hour_angle - minute_angle);
angle = Math.min( 360 -angle, angle);
return angle;
}
public static void main (String[] args)
{
System.out.println(calcAngle( 9 , 60 )+ " degree" );
System.out.println(calcAngle( 3 , 30 )+ " degree" );
}
}
|
Python3
def calcAngle(h,m):
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 ;
hour_angle = 0.5 * (h * 60 + m)
minute_angle = 6 * m
angle = abs (hour_angle - minute_angle)
angle = min ( 360 - angle, angle)
return angle
h = 9
m = 60
print ( 'Angle ' , calcAngle(h,m))
|
C#
using System;
class GFG {
static int calcAngle( double h, double m)
{
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;
}
int hour_angle = ( int )(0.5 * (h * 60 + m));
int minute_angle = ( int )(6 * m);
int angle = Math.Abs(hour_angle - minute_angle);
angle = Math.Min(360 - angle, angle);
return angle;
}
public static void Main ()
{
Console.WriteLine(calcAngle(9, 60));
Console.Write(calcAngle(3, 30));
}
}
|
PHP
<?php
function mintwo( $x , $y )
{
return ( $x < $y ) ?
$x : $y ;
}
function calcAngle( $h , $m )
{
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;
}
$hour_angle = 0.5 *
( $h * 60 + $m );
$minute_angle = 6 * $m ;
$angle = abs ( $hour_angle -
$minute_angle );
$angle = min(360 - $angle ,
$angle );
return $angle ;
}
echo calcAngle(9, 60), "\n" ;
echo calcAngle(3, 30), "\n" ;
?>
|
Javascript
<script>
function min(x, y)
{
return (x < y)? x: y;
}
function calcAngle(h, m)
{
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;
}
let hour_angle = 0.5 * (h * 60 + m);
let minute_angle = 6 * m;
let angle = Math.abs(hour_angle - minute_angle);
angle = min(360 - angle, angle);
return angle;
}
document.write(calcAngle(9, 60) + "<br>" );
document.write(calcAngle(3, 30) + "<br>" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Exercise: Find all times when hour and minute hands get superimposed.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 Jan, 2022
Like Article
Save Article