Archive for July, 2013

Find the Length of a given number C#

        public static long Length(this long number)
        {

            long length = 0;

            if (number < 10 && number >= 0)
            {
                length = 1;
                return length;
            }

            number = Math.Abs(number);

            while (number > 0)
            {
                length += 1;
                number = number / _base;
            }

            return length;
        }

Test Case

Length of number 123 is  3

Length of number 1234567890 is  10

Length of number -123456 is  6

Length of number 9 is  1

Length of number 545 is  3

Length of number 123454321 is  9

Length of number -123454321 is  9

Tree Traversal Pre-Order Iterative

We have already have post on various tree traversal in recursive way. Today we will cover how we can traverse a tree in iterative way.

Iterative Pre order tree traversal : For this we will be using stack to keep track of the traversed nodes

        public void iterativePreorder(TreeNode node)
        {

            bool done = false;
            Stack<TreeNode> parentStack = new Stack<TreeNode>();
            while (!done)
            {
                if (node == null && parentStack.Count() == 0)
                {
                    done = true;
                    return;
                }
                if (node != null)
                {
                    Console.Write(node.Data + " >");
                    parentStack.Push(node.Right);
                    node = node.Left;
                }
                else
                {
                    node = parentStack.Pop();
                }

            }
        }

Tree Traversal In-Order Iterative

We have already have post on various tree traversal in recursive way. Today we will cover how we can traverse a tree in iterative way.

Iterative in order tree traversal : For this we will be using stack to keep track of the traversed nodes.

        public void IterativeInorder(TreeNode node)
        {
            Stack<TreeNode> parentStack = new Stack<TreeNode>();

            bool done = false;
            while (!done)
            {
                if (node == null && parentStack.Count() == 0)
                {
                    done = true;
                    return;
                }
                if (node != null)
                {
                    parentStack.Push(node);
                    node = node.Left;
                }
                else
                {
                    var temp = parentStack.Pop();
                    Console.Write(temp.Data + " >");
                    node = temp.Right;
                }
            }
            Console.WriteLine();
        }

Tree Traversal In-order, Pre-order and Post-order:Recursive

public class TreeNode
    {
        public int Data { get; set; }
        public TreeNode Left { get; set; }
        public TreeNode Right { get; set; }
    }

In-Order

public void Inorder(TreeNode node)
        {
            if (node == null)
            {
                return;
            }
            Inorder(node.Left);
            Console.Write(node.Data + " >");
            Inorder(node.Right);
        }

Pre-Order

public void Preorder(TreeNode node)
        {
            if (node == null)
            {
                return;
            }
            Console.Write(node.Data + " >");
            Preorder(node.Left);
            Preorder(node.Right);
        }

Post-Order

public void Postorder(TreeNode node)
   {
       if (node == null)
       {
           return;
       }

       Postorder(node.Left);
       Postorder(node.Right);
       Console.Write(node.Data + " >");
   }

Given Matrix :

[1   14    25   35]
[2   16    28   38]
[5   20    28   40]
[16  22    38   41]  

search for 38.

public static void FindElementinSortedMatrix(int n)
        {

            int[,] myArray = {
                                 { 1, 14, 25, 35 },
                                 { 2, 16, 28, 38 },
                                 { 5, 20, 28, 40 },
                                 { 16, 22, 38, 41 }
                             };

            int i = 0;
            int j = myArray.GetLength(0) – 1;
            while (i < n && j >= 0)
            {
                if (myArray[i, j] == n)
                {
                    Console.WriteLine(string.Format("element Found at {0},{1}", i, j));
                    return;
                }
                else if (myArray[i, j] > n)
                {
                    j = j – 1;
                }
                else
                {
                    i = i + 1;
                }

            }
            Console.WriteLine("Element Not Found");
        }

Test Results

Element 38 Found at 1,3
Element 10 Not Found
Element -1 Not Found

Write a Program to Print Following Number Pattern

Input : Number of Rows

Output :

1
22
333
4444
55555

Code

        public static void PrintNumberPattern4(int rows)
        {
            for (int i = 1; i <= rows; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(i);
                }
                Console.WriteLine("");
            }
        }