# Find a time for which angle between hour and minute hands is given theta in C++

C++Server Side ProgrammingProgramming

Suppose we have one theta, or angle value. We have to find one time in hh:mm format, that creates the angle by the hour and minute hands. Suppose the angle is 90°, then the result can be 3:00.

As there are 12 hours, so there are 12 possibilities for hours and 60 possibilities for minutes. We will loop through all possible times. If angle for any time is same as given theta, then print that time.

## Example

Live Demo

#include<iostream>
#include<cmath>
using namespace std;
float angleFromClockHand(int hour, int minute) {
float hour_angle = 0.5 * (hour*60 + minute);
float minute_angle = 6*minute;
float angle = abs(hour_angle - minute_angle);
angle = min(360-angle, angle);
return angle;
}
void findTime(float theta) {
for (int hour=0; hour<12; hour++) {
for (int min=0; min<60; min++) {
if (angleFromClockHand(hour, min)==theta) {
cout << hour << ":"<< min;
return;
}
}
}
cout << "Unable to find time";
}
int main() {
float angle = 45.0;
findTime(angle);
}

## Output

4:30
Published on 18-Dec-2019 14:50:29