Monday, June 12, 2017

Working With Model View Controller - ASP.NET

Overview:

MVC is an architectural design pattern that separates an application into three main layers:

1. Model
2. View
3. Controller

Model
- Implements logic

View
- Displays UI

Controller
- Acts as User Interaction (similar to that of a button click in asp.net)
- Handles and responds to user input and interaction
- Works with model
- Selects a view to be rendered

MVC Life Cycle:

  •  User enters an URL in the browser and submits the request.
    • URLRoutingModule : - Entry point to MVC application
      • This is an HTTP module. It intercept the request and decides whether the request is handled by MVC application.
      • Selects the first matching route.
      • In Application_Start event of Global.asax.cs file, routes are added to the RouteCollection.
        • RouteConfig.RegisterRoutes(RouteTable.Routes);
        • Inside the RegisterRoutes method, routes.MapRoute method is used to map the specified URL route and sets default route values.



Saturday, June 10, 2017

C# - Factorial of a given number

In this article, we will calculate the factorial of a given number using C#

What is meant by factorial of a number?

The product of an integer and all the integers below it; e.g. factorial four ( 4! ) is equal to 24.

class Program
    {
        static void Main(string[] args)
        {

            #region Factorial

            Console.WriteLine("Factorial of :");
            int numm = int.Parse(Console.ReadLine());
            Console.WriteLine(PrintFactorial(numm));
            Console.ReadKey();

            #endregion

        }
#region Factorial

        private static double PrintFactorial(int num)
        {
            double fact = 1;

            #region using For Loop

            for (int i = num; i >= 1; i--)
            {
                fact = fact * i;
            }

            #endregion

            #region using While Loop

            while (num > 0)
            {
                fact = fact * num;
                num--;
            }

            #endregion

            return fact;
        }

        #endregion



}
Console Output:

Factorial of :
4
24

C# - Fibonacci Series

This post will show how we can print Fibonacci series using C# code.

Fibonacci Series:

a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc.


class Program
    {
        static void Main(string[] args)
        {
            #region Fibonacci

            Console.WriteLine("Fibonacci series upto :");
            int num = int.Parse(Console.ReadLine());
            Console.WriteLine(PrintFibonacci(num));
            Console.ReadKey();

            #endregion
        }

#region Fibonacci

        private static string PrintFibonacci(int num)
        {
            int firstNumber = 0;
            int secondNumber = 1;
            int tempNumber = 1;
            string stringFibonacci = "\n" + firstNumber.ToString() + "\n" + secondNumber.ToString();

            for (int i = 0; i < num; i++)
            {
                tempNumber = firstNumber + secondNumber;
                firstNumber = secondNumber;
                secondNumber = tempNumber;
                stringFibonacci += "\n" + tempNumber.ToString();
            }
            return stringFibonacci;
        }

  #endregion

}

Console Output:

Fibonacci series upto :
10

0
1
1
2
3
5
8
13
21
34
55
89

C# - check if a given number is an Armstrong Number

This article will demonstrate how to check a number is an Armstrong number in C#

Armstrong Number:
- Source Google

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371


class Program
    {
        static void Main(string[] args)
        {
            #region Armstrong Number

            Console.WriteLine("Enter a Number");
            Int32 number = int.Parse(Console.ReadLine());
            Console.WriteLine(CheckArmstrongNumber(number));
            Console.ReadKey();

            #endregion
        }

#region Armstrong Number

        private static string CheckArmstrongNumber(int number)
        {
            string msg = string.Empty;
            int rem = 0;
            int num = 0;
            int orgno = number;

            #region For Loop
            // Follow reverse the number technique and add the cubes of each digits          

            for (int i = number; i > 0; i = i / 10)
            {
                rem = i % 10;
                num += rem * rem * rem;
            }

            #endregion

            #region SecondWAY

            while (number > 0)
            {
                rem = number % 10;
                num += rem * rem * rem;
                number = number / 10;
            }

            #endregion

            if (orgno == num)
            {
                msg = "Entered Number is Armstrong Number.";
            }
            else
            {
                msg = "Entered Number is  not Armstrong Number.";
            }
            return msg;
        }

        #endregion

}

Console Output:

Enter a Number
371
Entered Number is Armstrong Number.






C# - Reverse a given number in c#

This post will help you reversing a number using c# code.

class Program
    {
        static void Main(string[] args)
        {
            #region Reverse a Number

            Console.WriteLine("Enter a Number");
            Int32 number = int.Parse(Console.ReadLine());
            Console.WriteLine("Reverse Of The Entered Number:" + ReverseNumber(number));
            Console.ReadKey();

            #endregion
         }

#region Reverse a Number

        private static string ReverseNumber(int number)
        {
            string revNumber = string.Empty;
            int rem = 0;

            #region using FOR loop
            // Start index from number, till it reaches greater than 0. Each time loop executes, divide i by                                                                                                                     // 10 and assign to itself.

            for (int i = number; i > 0; i = i / 10)
            {
                rem = i % 10; // get the remainder of the number and stores it into a new number
                revNumber += Convert.ToString(rem);
            }

            #endregion

            #region While Loop

            while (number > 0)
            {
                rem = number % 10;
                revNumber += Convert.ToString(rem);
                number = number / 10;
            }

            #endregion

            return revNumber;

        }

        #endregion

}

Console Output:

Enter a Number
1234567890
Reverse Of The Entered Number:0987654321





C# - Find a given character count in a string

In this article, we will find the count of a particular character in a given string using c#

class Program
    {
        static void Main(string[] args)
        {
            #region Find a given character count in a string

            Console.WriteLine("Enter a String :");
            string str = Console.ReadLine();
            Console.WriteLine("Enter the character:");
            char ch = Console.ReadLine();
            Console.WriteLine("The character " + ch + " occurs " + GetCharacterCount(str, ch) + " times in the given string");
            Console.ReadKey();

            #endregion
           
          }

        #region Find a given character count in a string

        private static int GetCharacterCount(string str, char ch)
        {
            int count = 0;
            int i = 0;

            // Search starts in the string from index (where the given character first occurs in the string at                // start index 0) and loops through entire string till the index not equals to -1
            while ((i = str.IndexOf(ch, i)) != -1)
            {
                i += ch.ToString().Length; // keep increasing the loop count
                count++;
            }
            return count;

        }

        #endregion
}

Console Output:

Enter a String :
AMRENDRA BAHUBALI
Enter the character:
A
The character A occurs 4 times in the given string

C# - Find each character occurance count in a string C#

In this article, we will find each character count in a given string in C#

class Program
    {
        static void Main(string[] args)
        {
            #region Find Each Char Count in a string

            Console.WriteLine("Please enter string");
            string input = Convert.ToString(Console.ReadLine());
            Dictionary objdict = GetAllCharOccurenceCount(input);
            foreach (var temp in objdict)
            {
                Console.WriteLine("Key: {0}, Value: {1}", temp.Key, temp.Value);
            }
            Console.ReadLine();

            #endregion
        }

      #region Find Each Char Count in a string
       ///

        /// This method will return a dictonary of string and int
        ///
        /// input string
        /// dictonary of string, int
        private static Dictionary GetAllCharOccurenceCount(string input)
        {
            Dictionary objdict = new Dictionary();
            foreach (char c in input)
            {
                if (objdict.ContainsKey(c.ToString()))
                {
                    objdict[c.ToString()] = objdict[c.ToString()] + 1; // this will increase the count of                                                                                                                // particular character in the dictionary
                }
                else
                {
                    objdict.Add(c.ToString(), 1); // first add the character and count as 1 in the dictionary;
                                                                   // if the character is found again, it will go to IF condition
                }
            }
            return objdict;
        }

        #endregion

}

CONSOLE OUTPUT:

Please enter string
NARENDRA DAMODARDAS MODI
Key: N, Value: 2
Key: A, Value: 5
Key: R, Value: 3
Key: E, Value: 1
Key: D, Value: 5
Key:  , Value: 2
Key: M, Value: 2
Key: O, Value: 2
Key: S, Value: 1
Key: I, Value: 1



C# - Check if a given string is Palindrome or not

In this article, we will write a program in C# which checks if the given string is a palindrome or not.

Question - What is palindrome?

Answer- A palindrome could be a word, phrase, or sequence that reads the same backwards as forwards, e.g. MADAM

class Program
    {
        static void Main(string[] args)
        {
            #region palindrome check

            Console.WriteLine("Enter a string to know if it is palindrome");
            string strPalindrome = Console.ReadLine();
            Console.WriteLine(CheckPalindrome(strPalindrome));
            Console.ReadKey();

            #endregion
         }
       
        #region Method To check a given string is palindrome or not

        private static string CheckPalindrome(string str)
        {
            string plndrmStr = string.Empty;
            string msg = string.Empty;

            #region OneWay
            // In this particular way, we will reverse the given string and store it into another string.
            // Now we will compare both the strings and check if the string is palindrome or not.

            for (int i = str.Length - 1; i != -1; i--)
            {
                plndrmStr += str[i];
            }
            if (str == plndrmStr)
            {
                msg = "Given String is Palindrome.";
            }
            else { msg = "Given String is not Palindrome."; }

            #endregion

            #region SecondWay
           // In this particular way, we will not be having another string to compare to the original one.
           // we are taking single FOR loop which proceeds forward and backward and checks if the //characters are equals

            for (int i = 0, j = str.Length - 1; i <= j && j != -1; i++, j--)
            {
                if (str[i] == str[j])
                {
                    msg = "Given String is Palindrome.";
                }
                else
                { msg = "Given String is not Palindrome."; break; }
            }

            #endregion

            return msg;
        }

        #endregion

    }
}

CONSOLE OUTPUT(s):

Enter a string to know if it is palindrome
MAN
Given String is not Palindrome.

Enter a string to know if it is palindrome
SOS
Given String is Palindrome.

Reverse a given string in C#

In C#, if you are given a string and asked to reverse that string, below code will be helpful for you.

INPUT => VIJAY PANDEY
OUTPUT => YEDNAP YAJIV

Step 1. Create a console application
Step 2. Follow below code

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please enter the string to be reversed :");
            string stringTobeReversed = Console.ReadLine();
            Console.WriteLine(GetReverseString(stringTobeReversed));
            Console.ReadKey();
        }

      #region Method to reverse a given string

        private static string GetReverseString(string str)
        {
            string revstr = string.Empty; // take an empty string to hold the reversed string

            #region using FOR LOOP
            // Start for loop from string length -1 and proceed till the index not equals to -1.
            // Continue decreasing the index

            for (int i = str.Length - 1; i != -1; i--)
            {
                revstr += str[i]; // append each character of the string to the empty string we already have
            }
            return revstr;

            #endregion

            #region Using WHILE LOOP
            // Similarly, we can do this using While loop
            int length = str.Length - 1;
            revstr = string.Empty;
            while (length >= 0)
            {
                revstr += str[length];
                length--;
            }
            return revstr;

            #endregion
        }

        #endregion

   }


SQL INTERVIEW QUESTIONS (SCENARIO BASED) - Part 1

FINDING Second Highest Salary in SQL CREATE TABLE #EMP ( NAME NVARCHAR ( 100 ), SALARY BIGINT ) INSERT INTO #EMP VALUES (...