Archive for the ‘ Algorithm ’ Category

In a clock, calculate the angle between hour and minute handle

A method to solve such problems is to consider the rate of change of the angle in degrees per minute. The hour hand of a normal 12-hour analogue clock turns 360° in 12 hours (720 minutes) or 0.5° per minute. The minute hand rotates through 360° in 60 minutes or 6° per minute.

Equation for the angle of the hour hand

ΘH = (60H+M)/2

 

Equation for the degrees on the minute hand

ΘM= 6M

M= Minute and H = Hour

Angle between Hour and minute handle

ΘH – ΘM

Co-ordinate of triangle: A(x1, y1), B(x2, y2), C(x3, y3) and Point P (x, y).

Approach:

  1. Calculate the area triangle that is are of triangle ABC. Area A
  2. Calculate the area of PAB. Area A1
  3. Calculate the area of PBC. Area A2
  4. Calculate the area PCA. Area A3
  5. If Point P lies outside the triangle than A1+A2+A3 >A.

Area of Triangle: [x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2)]/2

In above approach, we will have precision problem because we are dividing the result by 2. So to handle this issue, by taking off 2 times of triangle are.

Write a Program to Return Excel Sheet Column Header Name if Input is an Number

Write a Program to Return Excel Sheet Column Header Name if Input is a Number

input

           -11
              1
            26
            52
            28
           702
           703

output

input number -11 is invalid
input number 1 , output alphabet A
input number 26 , output alphabet Z
input number 52 , output alphabet AZ
input number 28 , output alphabet AB
input number 702 , output alphabet ZZ
input number 703 , output alphabet AAA

 public static string ConvertToAlphabet(int number)
        {
            int temp = number;
            char[] alphabets = new char[] { 'A', 'B', 'C', 'D', 'E', 
                                            'F', 'G', 'H', 'I', 'J', 
                                            'K', 'L', 'M', 'N', 'O', 
                                            'P', 'Q', 'R', 'S', 'T', 
                                            'U', 'V', 'W', 'X', 'Y', 
                                            'Z' };
            string Alphabet = string.Empty;
            int Index = 0;

            if (number <= 0)
            {

                Console.WriteLine("input number {0} is invalid", temp);

                return string.Empty;
            }

            if (number < 27)
            {

                Console.WriteLine("input number {0} , output alphabet {1}"
                    , temp, alphabets[number - 1]);

                return alphabets[number - 1].ToString();
            }
            while (number > 0)
            {
                Index = number % 26;
                number = number / 26; ;

                if (Index == 0)
                {
                    Index = 26;
                    number = number - 1;
                }
                Alphabet = alphabets[Index - 1] + Alphabet;

            }

            Console.WriteLine("input number {0} , output alphabet {1}"
                , temp, Alphabet);
            return Alphabet;
        }