Monday, November 11, 2013

Phone Numbers Comparison in Different Country Formats


In every country phone numbers are represented in its own format. For example : In some country a phone number could be in (914) 575-4231 format whereas in another country the same number could be in 914-575-42131 format. So if we want to compare two numbers represented in different format, we could do this by removing their formatting and compare their actual value.


using System;
using System.Collections;
 
class Program
{
 
    static void Main()
    {
        string num1 = "800-555-1212"; // Format -1
        string num2 = "(800) 555 1212"; // Format -2
 
        if (FetchDigitsOnlyFromPhoneNumber(num1) ==
        FetchDigitsOnlyFromPhoneNumber(num2))
        {
            Console.WriteLine("Number in both the formats are Equal !");
        }
        else
        {
            Console.WriteLine("Unequal Numbers !");
        }
        Console.Read();
    }
 
    private static string FetchDigitsOnlyFromPhoneNumber(string formattedNumber)
    {
        string actualNumber;
 
        actualNumber = formattedNumber;
        actualNumber = actualNumber.Replace("(", string.Empty);
        actualNumber = actualNumber.Replace(")", string.Empty);
        actualNumber = actualNumber.Replace("+", string.Empty);
        actualNumber = actualNumber.Replace("-", string.Empty);
        actualNumber = actualNumber.Replace(" ", string.Empty); //Blank
        actualNumber = actualNumber.Replace(".", string.Empty);
        return actualNumber;
    }
}

One liner Factorial calculating program using recursion in C#


using System;
 
public class Program
{
    //Using recursion
    static long Factorial(long number)
    {
        return ((number <= 1) ? 1 : number * Factorial(number - 1));
    }
 
    static void Main(string[] args)
    {
        long numToFindFact = 6;
        Console.WriteLine("The factorial of " + numToFindFact  + " is: {0}\n", Factorial(numToFindFact));
    }
}