Factorial Calculator using C#

Factorial is a positive number is the product of all number less than or equal to the positive number.
For example:

0! = 1
1! = 1
2! = 2 * 1! = 2
3! = 3 * 2! = 6

n! = n * (n – 1)!

Here is the sample code to calculate Factorial using both loop and recursion using c#.


///

/// Author: Md. Mahedee Hasan
///

class Program
{
static void Main(string[] args)
{
int number = 0;
Console.WriteLine(“Enter an integer number to calculate factorial: “);
Int32.TryParse(Console.ReadLine(), out number);

Console.WriteLine(“Factorial of ” + number + ” is: ” + CalculateFactoial(number));
Console.WriteLine(“Factorial of ” + number + ” is: ” + Factorial(number));
Console.ReadLine();
}

//Calculate factorial using loop

private static int CalculateFactoial(int number)
{
int factorial = 1;
for (int counter = 1; counter <= number; counter++) { factorial = factorial * counter; } return factorial; } //Calculate factorial using recursion private static int Factorial(int number) { if (number == 0) return 1; else return number * Factorial(number – 1); } } [/csharp]

How to check Palindrome word using C#

Palindrome word can be read in both direction. Suppose “level” can be read in both direction, so it is palindrome word. To check palindrome word you can try like following code using C#.


    class Program
    {
        public static bool IsPalindrome(string word)
        {
            int minLength = 0;
            int maxLength = word.Length - 1;
            while (true)
            {
                if (minLength > maxLength)
                {
                    return true;
                }
                char a = word[minLength];
                char b = word[maxLength];
                if (char.ToLower(a) != char.ToLower(b))
                {
                    return false;
                }
                minLength++;
                maxLength--;
            }
        }

        static void Main()
        {
            string[] palindromeWord = {"aibohphobia","Dhaka","alula","cammac","Civic", "deified","deleveled",
                                 "detartrated","devoved","History","evitative","level","","mahedee"};

            foreach (string word in palindromeWord)
            {
                if (IsPalindrome(word))
                {
                    Console.WriteLine(word + " is a Palindrome");
                }
                else
                {
                    Console.WriteLine(word + " is not a Palindrome");
                }
            }
            Console.ReadKey();
        }
    }