- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Find a time for which angle between hour and minute hands is given theta in C++
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
#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
Advertisements