My sharing and discussion of topics in C#, WCF, WPF, Winforms, SQL, ASP.Net, Windows Service, Java Script .... with you all.
Thursday, August 7, 2014
How can a totally logical machine like a computer generate a random number?
Have you wondered how a random number is generated in programming languages on a totally logical device like a Computer?
Recently I read an article about it which throws a light on this interesting topic.
Here is the link:
http://computer.howstuffworks.com/question697.htm
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));
}
}
Subscribe to:
Posts (Atom)