Showing posts with label C# Programs. Show all posts
Showing posts with label C# Programs. Show all posts

Tuesday, September 11, 2012

Swap string without temp variable


string a1 = "Prasanth";
string a2 = "Sethupathy";

int aa = a1.Length;

a1 = a2 = a1 + a2;
a2 = a2.Substring(0, aa);

a1 = a1.Substring(aa);

Monday, September 10, 2012

Write C# method to display second largest and second smallest number in a given array.

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();
        }
    }
}

Write C# method to find number of vowel character in the given sentence

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 reverse the input string without using String.Reverse() method

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
                Console.Write(a[i]);
            Console.ReadKey();
        }
    }
}

Write c# method to swap 2 numbers without using the third/temp variable

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 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.


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

namespace ConsoleApp
{
    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();
        }
    }
}