- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Angle Between Hands of a Clock in C++

Suppose we have two numbers, hour and minutes. We have to find a smaller angle (in sexagesimal units) formed between the hour and the minute hand. So if the input is like hour = 12 and min := 30, then the result will be 165°.

To solve this, we will follow these steps −

if h = 12, then set h := 0

if m = 60, then set m := 0

hAngle := 0.5 * (60h) + m

mAngle := 6m

ret := |hAngle - mAngle|

return minimum of ret and (360 – ret)

## Example (C++)

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; class Solution { public: double angleClock(int h, int m) { if(h == 12) h = 0; if(m == 60) m = 0; double hAngle = 0.5*((60 * h) + m); double mAngle = 6 * m; double ret = abs(hAngle - mAngle); return min(360 - ret, ret); } }; main(){ Solution ob; cout << (ob.angleClock(12, 30)); }

## Input

12 30

## Output

165.00000

- Related Articles
- Program to find angle between hour and minute hands of a clock in C++?
- Find the angle measure between the hands of the clock in each figure:"
- Find a time for which angle between hour and minute hands is given theta in C++
- Through what angle does the hour hand of a clock turn in 20minutes?
- What is the smaller angle between the minute and hour hands at 12:46 AM? Â
- Couples Holding Hands in C++
- Angle between a chord and a tangent when angle in the alternate segment is given in C++?
- Convert time from 24 hour clock to 12 hour clock format in C++
- What is the difference between a Stopwatch and a Stop-clock?
- Angle between two Planes in 3D in C++?
- C++ Program to find maximum possible smallest time gap between two pair of clock readings
- How to use clock() function in C++
- Angle between two Planes in 3D in C Program?
- In a quadrilateral $A B C D$, $C O$ and $D O$ are the bisectors of $angle C$ and $angle D$ respectively. Prove that $angle C O D=frac{1}{2}(angle A+angle B)$
- The short and long hands of a clock are $4 cm$ and $6 cm$ long respectively. Find the sum of distances travelled by their tips in 2 days. $[ take pi=frac{22}{7} ]$.

Advertisements