TechoSagar: Angle between hour and minute hand

Search

Google Alert - jobs

Angle between hour and minute hand

Calculate the angle between hour hand and minute hand.
There can be two angles between hands, we need to print minimum of two. Also, we need to print floor of final result angle. For example, if the final angle is 10.61, we need to print 10.

Input:
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of one line conatining two space separated numbers h and m where h is hour and m is minute.
Output:
Coresponding to each test case, print the angle b/w hour and min hand in a separate line.

Constraints:
1 ≤ T ≤ 100
1 ≤ h ≤ 12
1 ≤ m ≤ 60

Example:
Input
2
9 60
3 30
Output
90
75
Your Code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
float h,m;
cin>>h>>m;
float x=h*5;
float y=m-x;
float z=y*6;
float r;
int p;
if(m==60)
{  
   r=360-z;
   p=min(r,z);
   if(p<0)
   p=-p;
   
cout<<p<<endl;
}
else
{
float w=(float)m*0.5;
float ans=(z-w);
// cout<<ans;
if(ans<0)
ans=-ans;
r=360-ans;
p=min(r,ans);
cout<<p<<endl;
   }
}
}

Follow us

Follow Bijendra Kumar on Facebook