C# Code Snippet

1.Factorial Of A Number


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BackToBasic
{
    class FactorialNumber
    {
       
        static int Factorial(int number)
        {
            if (number > 0)
            {
                if (number == 1)
                {
                    return 1;
                }
                else
                {
                    return (number * Factorial(number - 1)); //Recursive call
                }
            }
            else
                return 0;
        }
        public static void Main()
        {
            int iNumber = 0;
            Console.WriteLine("Enter a number ?");
            iNumber = int.Parse(Console.ReadLine());
            int iFactorial = Factorial(iNumber);
            Console.WriteLine(string.Format("Factorial of {0} is {1}", iNumber, iFactorial));
            Console.ReadLine();
        }
    }
}

 

Explanation:-

In this above demo i am finding Factorial of number by using recursion mechanism
What is Recursion ?
Recursion is a process in which we calling the same method repetitively.

 

Note:You can achieve this by using different mechanism

2.String Palindrome


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BackToBasic
{
    class StringPaliendrom
    {
        static bool CheckPailendrom(string strPailndrom) {
            string strOriginalString = strPailndrom;
            int count=0;
            for (int i = strPailndrom.Length-1; i >= 0; i--)
            {
                if (strPailndrom[i] == strOriginalString[count])
                {
                    count++;
                    continue;
                }
                else
                {
                    return false;
                }
            }
            return true;
        }
        public static void Main() {
            string strPailendrom = string.Empty;
            Console.WriteLine("Enter a string :");
            strPailendrom = Console.ReadLine();
           bool isPailendrom=CheckPailendrom(strPailendrom);
           if (isPailendrom)
           {
               Console.WriteLine(string.Format("String {0} is Palindrome.", strPailendrom));
           }
           else
           {
               Console.WriteLine(string.Format("String {0} is not Palindrome.", strPailendrom));
           }
           Console.ReadLine();
        }
    }
}

 

Explanation:-

In this above demo we are see that how to find palindrome of as string.
What is Palindrome ?
Palindrome means the original element same as reverse element.
You can find Palindrome for number as well.
So when the original element same as it’s reverse then particular element is called as palindrome.
For eg:- aba.

 

Note:You can also achieve this by using different mechanism.

3.Second smallest digit in a given Number


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BackToBasicProj
{
    class SecondSmallestDemo
    {
        public static void Main()
        {
        Print:
            Console.WriteLine("Enter Your Number ?");
            long no = long.Parse(Console.ReadLine());
           
           FindsecondSmallest(no);

            Console.WriteLine("R u want to Continue ? Y/N");
            string bdata = Console.ReadLine();
            if (bdata=="Y")
            {
                goto Print;
            }
           
            Console.ReadLine();
        }

        private static void FindsecondSmallest(long no)
        {
            long smallest = no % 10;
            long third = 0;
            
            no = no / 10;
            while (no>0)
            {
                long r = no % 10;
                no = no / 10;
                if (r<smallest)
                {
                    third = smallest;
                    smallest = r;
                    
                }
                else
                {
                    if (third==0)
                    {
                        third = r;
                    }
                    else
                    {
                        if (r<third)
                        {
                            third = r;
                            if (third<smallest)
                            {
                                smallest = third;
                            }
                        }
                    }
                }
            }
            Console.WriteLine("Second Smallest is {0}", third);
        }
    }
}

 

Explanation:-

If you give input as :- 752845031
Output:- Second Smallest Number is 1

 

4.Second largest digit in a given Number


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BackToBasicProj
{
    class NewTestDemo
    {
        public static void Main()
        {
        Print:
            Console.WriteLine("Enter Your Number ?");
            long no = long.Parse(Console.ReadLine());
           
           FindsecondLargest(no);

            Console.WriteLine("R u want to Continue ? Y/N");
            string bdata = Console.ReadLine();
            if (bdata == "Y")
            {
                goto Print;
            }
                      
           
        }

        private static void FindsecondLargest(long no)
        {
            long smallest = no % 10;
            long third = 0;
            
            no = no / 10;
            while (no>0)
            {
                long r = no % 10;
                no = no / 10;
                if (r>smallest)
                {
                    third = smallest;
                    smallest = r;
                    
                }
                else if (r == smallest)
                {
                    smallest = r;
                }
                else
                {
                    if (third==0)
                    {
                        third = r;
                    }
                    else
                    {
                        if (r>third)
                        {
                            third = r;
                            if (third>smallest)
                            {
                                smallest = third;
                            }
                        }
                    }
                }
            }
            Console.WriteLine("Second Largest is {0}", third);
          
        }
    }
}


 

Explanation:-

If you give input as :- 752845031
Output:- Second Largest Number is 7

 

Advertisements