Powered By Blogger

Monday, July 23, 2012

Some Useful C Sharp Programs for Interview



Write C# method for ATM money disposal, which take integers in multiple of 100’s as a input and print number denomination (1000’s, 500’s and 100’s) in it.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication12
{
    class Program
    {
        static void Main(string[] args)
        {
            int x, y, z, c = 0;
            Console.WriteLine("enter the number");
            x = int.Parse(Console.ReadLine());
            {
                y = x / 1000;
                z = (x % 1000) / 500;
                c = ((x % 1000) % 500) / 100;
            }
            Console.WriteLine("the 1000 is {0}", y);
            Console.WriteLine("the 500 is:" + z);
            Console.WriteLine("the 100 is {0}", c);
            Console.ReadKey();
        }
    }
}

Write c# method to swap 2 numbers without using the third/temp variable
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace swap
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 30;
            a = a + b;
            b = a - b;
            Console.WriteLine("the value of b:" + b);
            a = a - b;
            Console.WriteLine(a);
        }
    }
}

Write C# method to reverse the input string without using String.Reverse() method
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

            char[] a = new char[10];
            Console.WriteLine("enter a string(Max 10 char):");
            a = Console.ReadLine().ToCharArray();
            int len, i;
            char temp;
            len = a.Length - 1;
            for (i = 0; i < a.Length / 2; i++)
            {
                temp = a[i];
                a[i] = a[len];
                a[len--] = temp;
            }
           
            Console.WriteLine("the ordered array");
            for(i=0;i<a.Length;i++)
                Console.Write(a[i]);
            Console.ReadKey();
        }
    }
}

Write C# method to find number of vowel character in the given sentence
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace vowel_count
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] str = new char[10];
            Console.WriteLine("enter a string(Max 10 char):");
            str = Console.ReadLine().ToCharArray();
            int i = 0;
            int vowels=0;
            for (i = 0; i < str.Length; i++)
            {
                if (str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U' ||
                    str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')
                {
                    vowels++;
                 
                  
                }
        }

                Console.WriteLine("the number of vowels {0}", vowels);
                Console.ReadKey();
           
        }

Write C# method to display all the prime number exist between two input numbers
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
           int i, prime;
            int lim_up=0;
            int lim_low=0;
            int n;
            Console.WriteLine("Enter the lower limit {0}",lim_low);
            lim_low = int.Parse(Console.ReadLine());
             Console.WriteLine("Enter the lower limit {0}",lim_up);
             lim_up = int.Parse(Console.ReadLine());
            Console.WriteLine("the prime numbers are:");



for(n=lim_low+1; n<lim_up; n++)
{
prime = 1;
for(i=2; i<n; i++)
if(n%i == 0)
{
prime = 0;
break;
}
if (prime!=0)
    Console.WriteLine("the prime number {0}", n);
}
Console.ReadKey();
        }
    }
}

Write C# method to display second largest and second smallest number in a given array.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

            int temp;
            int[] sort = new int[5];
            Console.WriteLine("enter the number");
            for (int i = 0; i < sort.Length; i++)
                sort[i] = int.Parse(Console.ReadLine());
            for (int i = 0; i < sort.Length; i++)
            {
                for (int j = i + 1; j < sort.Length; j++)
                {
                    if (sort[i] > sort[j])
                    {
                        temp = sort[i];
                        sort[i] = sort[j];
                        sort[j] = temp;
                    }
                }
            }
            Console.WriteLine("the output");
            for (int i = 0; i < sort.Length; i++)
                Console.WriteLine(sort[i]);

            Console.WriteLine("the second smallest number is {0}", sort[1]);
            Console.WriteLine("the second largest number is {0}", sort[3]);
            Console.ReadKey();

        }

    }
}

Fibonacci Series Program
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace fibonacci
{
    class Program
    {
        static void Main(string[] args)
        {
            int f1 = 0, f2 = 1, f3, limit, count;
            Console.WriteLine("enter the limit");
            limit = int.Parse(Console.ReadLine());
            Console.WriteLine("the fibonacci series is:");
            Console.WriteLine("{0} {1}", f1, f2);
            count = 2;
           
            while (count <limit)
            {
                f3 = f1 + f2;
                f1 = f2;
                f2 = f3;
                count++;
                Console.WriteLine("{0}", f3);
               
            }
        }
    }
}

Palindrome Check Program.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Palindrome
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] str = new char[10];
            Console.WriteLine("enter the palindrome");
            str = Console.ReadLine().ToCharArray();
            Console.Write(CheckPalindrome(str));
            Console.WriteLine(str);
            Console.ReadLine();
        }
        private static bool CheckPalindrome(char[] mystring)
        {
            int startchar;
            int lastchar;
            startchar = 0;
            lastchar = mystring.Length - 1;
            while (startchar < lastchar)
            {
                if (mystring[startchar] == mystring[lastchar])
                {
                    startchar++;
                    lastchar--;
                }
                else
                {
                    return false;
                }
            }
            return true;

        }
    }
}

No comments:

Post a Comment