Finding angle between two hands in the clock using Console App

Finding angle between two hands in the clock using Console Application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClockHandsAngle
{
    class Program
    {
        static void Main(string[] args)
        {  
            /* Once user enters the Hours and Minutes, convert them into integer values.
             * Since for one full rotation, minutes hand takes 60 minutes and completes
             * 360 degrees, each minute will be about 6 degrees. Now check the difference
             * between hours hand and minutes hand. (Hours hand value will be equal to 5
             * times number of minutes). This will help get the difference between minutes
             * and hours hand. For 12'0 clock, we need to reset the number of hours to 0.*/
           
            Console.WriteLine("Enter the Hours : ");
            int hours = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the Minutes : ");           
            int minutes = Convert.ToInt32(Console.ReadLine());
            //each hour is multiplied by 5 to get a cumulative hour value (which is more like             //converting it into minutes
            int cumulativehours = hours * 5;
            int difference = 0;
            if (hours == 12)
            {
                cumulativehours = 0;
            }
            if (minutes >= cumulativehours)
            {
                difference = minutes - cumulativehours;
            }
            else
            {
                difference = cumulativehours - minutes;
            }          

            int totaltime = 6 * difference;
            Console.WriteLine("The angle between Hours hand and Minutes hand for {0}:{1} is               {2} degrees", hours,minutes,totaltime);
            Console.ReadKey();
        }
    }
}

Output :


No comments: