Sunday 26 September 2021

Sum two number without arithmetic operator

Sum two number without arithmetic operator, Using C#


using System; 

namespace ConsoleApp1

{

    public class Program

    {

        static void Main()

        {

            int sum = SumWithoutArithmeticOprator(25, 7);

            Console.WriteLine($"Sum of two numbers is {sum}");

            Console.ReadLine();

        }

 

        private static int SumWithoutArithmeticOprator(int v1, int v2)

        {

            if (v2 == 0) return v1;

            if (v1 == 0) return v2;

            // Where v1 ^ v2: XOR bitwise calculation

            // v1 & v2: AND bitwise with carry

            return SumWithoutArithmeticOprator((v1 ^ v2), (v1 & v2) << 1);

        }

    }

}

 

 






Thursday 23 September 2021

Binary Search Tree

 



Simple & easy way to create binary search tree in C# or .net Core



using System; 

namespace BinarySearchTree

{

    public class Program

    {

        static void Main()

        {

            Node root = null;

            BinaryTree binaryTree = new BinaryTree();

 

            root = binaryTree.Insert(root, 5);

            root = binaryTree.Insert(root, 4);

            root = binaryTree.Insert(root, 2);

            root = binaryTree.Insert(root, 3);

            root = binaryTree.Insert(root, 7);

            root = binaryTree.Insert(root, 6);

            root = binaryTree.Insert(root, 8);

 

            binaryTree.Traverse(root);

 

            Console.ReadLine();

        }

 

    }

 

    class Node

    {

        public int value;

        public Node left;

        public Node right;

    }

 

    class BinaryTree

    {

        internal Node Insert(Node root, int value)

        {

            if (root is null)

            {

                root = new Node

                {

                    value = value

                };

            }           

            else if (value < root.value)

            {

                root.left = Insert(root.left, value);

            }

            else

            {

                root.right = Insert(root.right, value);

            }

 

            return root;

        }

 

        public void Traverse(Node root)

        {

            if (root is null) return; 

            Traverse(root.left);

            Console.Write($"{root.value} ");

            Traverse(root.right);

        }

    }

}

 

 


C# Number double decimal convert into culture spacific or time zone

string value;

            System.Globalization.NumberStyles style;

            System.Globalization.CultureInfo culture;

            float number;

 

            value = "1.345,978";

            style = System.Globalization.NumberStyles.AllowDecimalPoint |

                    System.Globalization.NumberStyles.AllowThousands;

            culture = System.Globalization.CultureInfo.CreateSpecificCulture("es-ES");

            if (Single.TryParse(value, style, culture, out number))

                Console.WriteLine("Converted '{0}' to {1}.", value, number);

            else

                Console.WriteLine("Unable to convert '{0}'.", value);

 

            value = "1345,978";

            if (Single.TryParse(value, style, culture, out number))

                Console.WriteLine("Converted '{0}' to {1}.", value, number);

            else

                Console.WriteLine("Unable to convert '{0}'.", value);

 

            var convertBack = number.ToString(culture);