C# Program to Find the Angle Between the Clock Needles
Most of the IT companies check the coding skills and problem-solving skills as well along with the theoretical interview questions. Sometimes you are free to write the pseudo code and sometimes you are asked to write the complete program either on any paper or any editor.
This question is asked in the coding interview to write the program. Here you can find the program as well as a live running program so that you can test the program immediately.
Question:
How can you find the angle between the hour and minute hand (needle) of a clock at any given time? Write a program for the same.
The logic is to find the difference in the angle of the hour and minute hand from the position of 12:00 when the angle between them is zero.
Each hour on the clock represents an angle of 30 degrees (360 divided by 12).
Similarly, each minute on the clock will represent an angle of 6 degrees (360 divided by 60) and the angle for an hour will increase as the minutes for that hour increases.
C# Program To Find the Angles between Hrs and Minute Needle of Clock
// Author : Tech Point Fundamentals
// Website : www.techpointfunda.com
// Channel : https://www.youtube.com/c/TechPointFundamentals
// Demo : Finding Angle between Hrs and Minute Clock Niddle
using System;
public class Program
{
public static void Main()
{
int inputHrs = 9;
int inputMin = 0;
Console.WriteLine("Input Time : " + inputHrs + "." + inputMin);
Console.WriteLine("Clock Angle : " + AngleInClokTime(inputHrs, inputMin));
}
public static double AngleInClokTime(int hrs, int mins)
{
double hourDegrees = (hrs * 30) + (mins * 30.0 / 60);
double minuteDegrees = mins * 6;
double difference = Math.Abs(hourDegrees - minuteDegrees);
if (difference > 180)
{
difference = 360 - difference;
}
return difference;
}
}
Output:
Input Time : 9.0
Clock Angle : 90
No comments:
Post a Comment
Please do not enter any HTML. JavaScript or spam link in the comment box.