Tuesday 29 January 2013

Logical Interview Questions Using C#

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.swap(); Console.WriteLine();

            obj.Factorial(); Console.WriteLine();

            obj.Fabbonic(10); Console.WriteLine();

            obj.TriUpToDown(); Console.WriteLine();

            obj.TriDownToUp(); Console.WriteLine();

            obj.InterchageNebhorVaules(); Console.WriteLine();

            obj.InterchageFirstToLastSoOn(); Console.WriteLine();

            obj.xMasTree(); Console.WriteLine();

            obj.LeapYear(2016); Console.WriteLine();

            Console.ReadLine();
        }

        protected void swap()
        {
            int a = 5, b = 6;
            Console.WriteLine(a.ToString() + "  " + b.ToString());
            b = a + b;
            a = b - a;
            b = b - a;
            Console.WriteLine(a.ToString() + "  " + b.ToString());
        }

        protected void Factorial()
        {
            int a = 7;
            int c = 1;
            for (int i = 1; i <= a; i++)
            {
                c = c * i;
            }
            Console.WriteLine(c.ToString());
        }

        protected void Fabbonic(int till)
        {
            int a = 1, b = 1;
            int c;
            Console.Write(a.ToString() + " " + b.ToString() + " ");
            for (int i = 1; i < till; i++)
            {
                c = a + b;
                a = b;
                b = c;
                Console.Write(c.ToString() + " ");
            }
        }

        protected void TriUpToDown()
        {
            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
        }

        protected void TriDownToUp()
        {
            for (int i = 0; i < 6; i++)
            {
                for (int j = i; j < 6; j++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }
        }

        protected void xMasTree()
        {
            int a = 0;
            for (int i = 0; i < 8; i++)
            {
                for (int k = i; k < 8; k++)
                {
                    Console.Write(" ");
                }
                for (int j = 0; j < i; j++)
                {
                    Console.Write("* ");
                }
                Console.WriteLine();
                a++;
                if (a == 3 || a == 7)
                    i = 1;
            }
        }

        protected void InterchageNebhorVaules()
        {
            string input = "NITISH";
            Char[] arrInput = input.ToCharArray();

            StringBuilder output = new StringBuilder();
            for (int i = 0; i < arrInput.Length / 2; i++)
            {
                output.Append(arrInput[i + 1].ToString());
                output.Append(arrInput[i].ToString());
            }

            Console.WriteLine(input + " >>  " + output.ToString());
        }

        protected void InterchageFirstToLastSoOn()
        {
            string input1 = "NITISH";
            StringBuilder output1 = new StringBuilder();
            StringBuilder output2 = new StringBuilder();

            char[] characters1 = input1.ToCharArray();

            for (int i = 0; i < characters1.Length / 2; i++)
            {
                output1.Append(characters1[characters1.Length - (i + 1)]);
                output2.Append(characters1[i]);
            }
            Console.Write(input1.ToString() + " >--> " + output1.ToString() + Operation.ReverseString(output2.ToString()));
        }

        protected void LeapYear(int yr)
        {            
            if ((yr % 4 == 0 && yr % 100 != 0) || (yr % 400 == 0))
            {
                Console.WriteLine(yr.ToString() + " is a leap year.");
            }
            else
            {
                Console.WriteLine(yr.ToString() + " is not a leap year.");
            }
            Console.Read();
        }
    }

    static class Operation
    {
        /// <summary>
        /// Receives string and returns the string with its letters reversed.
        /// </summary>
        public static string ReverseString(string s)
        {
            char[] arr = s.ToCharArray();
            Array.Reverse(arr);
            return new string(arr);
        }
    }
}

2 comments:

  1. For 3rd question, below is the better answer...

    private static void xmastree1()
    {
    int k = 10;
    for (int j = 0; j < k; j++)
    {

    for (int i = 0; i <= (j + k); i++)
    {
    if (i >= (k - j) && i <= (k + j))
    {
    Console.Write("*");
    Console.Write("\t");
    }
    else
    {
    Console.Write("\t");
    }
    }
    Console.Write("\n");
    }

    }

    ReplyDelete
  2. Dear Anandam,

    I think you should run this program again.

    ReplyDelete