Saturday, September 28, 2013

Important Array Programs in C#

Find Sum and Average an Array

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

namespace SumAndAverage
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num=new int[500];
            int negativesum=0, positivesum=0, total=0,avg=0;

            Console.WriteLine("Enter the total number of element in an Array:\t");
            int n = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the value one by one");
            for (int i = 0; i < n; i++)
            {
                num[i] = Convert.ToInt32(Console.ReadLine());
            }

            for (int i = 0; i < n; i++)
            {
                if (num[i] < 0)
                {
                    negativesum += num[i];
                }
                else if (num[i] > 0)
                {
                    positivesum += num[i];
                }
                else if(num[i]==0)
                {
                    ;
                }
                total += num[i];
                
            }
            avg = total / n;

            Console.WriteLine("The Positive Sum Value:=" + positivesum);
            Console.WriteLine("The Negative Sum Value:=" + negativesum);
            Console.WriteLine("The Sum of Array Value:=" + total);
            Console.WriteLine("The Average of Array Value:=" + avg);
            Console.ReadLine();
        }
    }
}

Output

Find Sum and Average an Array


Friday, September 27, 2013

String to ASCII Convertion

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

namespace ASCII2Number
{
    class Program
    {
        static void Main(string[] args)
        {
            string str;
            Console.WriteLine("Enter the String to convert ASCII:\t");
            str = Console.ReadLine();
            int length=0;

            foreach (char obj in str)
            {
                length++;
            }
            for (int i = 0; i < length; i++)
            {
                Console.Write(Convert.ToInt32(str[i])+" ");
            }
                Console.ReadLine();
        }
    }
}

Output

String to ASCII Convertion

String Lower to Upper without using String Functions

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

namespace Lower2Upper
{
    class Program
    {
        static void Main(string[] args)
        {
            string lower, upper = "";
            int length = 0;

            Console.WriteLine("Enter the LowerCase String:\t");
            lower = Console.ReadLine();

            foreach (char obj in lower)
            {
                length++;
            }
            Console.WriteLine("The length of String:\t"+length);

            for (int i = 0; i < length; i++)
            {
                if (lower[i] >= 97 && lower[i] <= 122)
                {
                    upper += (char)(lower[i] - 32);
                }
                else
                {
                    upper += lower[i];
                }
            }
            Console.WriteLine("The LowerCase to UpperCasse String:\t"+upper);
            Console.ReadLine();

        }
    }
}

Output

String Lower to Upper without using String functions


String Upper to Lower without using String Functions

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

namespace Upper2Lower
{
    class Program
    {
        static void Main(string[] args)
        {
            string upper, lower = "";
            int length=0;
            Console.WriteLine("Enter the UpperCase String:\t");
            upper = Console.ReadLine();

            foreach(char obj in upper)
            {
                length++;
            }
            Console.WriteLine("String Length:\t"+length);

            for (int i = 0; i < length; i++)
            {
                if (upper[i] >= 65 && upper[i] <= 90)
                {
                    lower += (char)(upper[i] + 32);
                }
                else
                {
                    lower += upper[i];
                }
            }

            Console.WriteLine("The UpperCase to LowerCase String:\t"+lower);
            Console.ReadLine();

        }
    }
}

Output

String Upper to Lower without using String Functions



String Compare without using String Function

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

namespace CompareString
{
    class Program
    {
        static void Main(string[] args)
        {
            int count = 0, len1 = 0,len2=0;
            Console.WriteLine("\nEnter the String 1");
            string str1 = Console.ReadLine();
            Console.WriteLine("\nEnter the String 2");
            string str2 = Console.ReadLine();          

            foreach (char obj in str1)
            {
                len1++;
            }
            Console.WriteLine("\nString 1 Length:\t"+len1);

            foreach (char obj in str2)
            {
                len2++;
            }
            Console.WriteLine("\nString 2 Length:\t"+len2);

            if (len1==len2)
            {
                for (int i = 0; i < len1; i++)
                {
                    if (str1[i] == str2[i])
                    {
                        count++;
                    }
                    else
                    {
                        count=0;
                    }
                }
            }
            if (count==len1)
            {
                Console.WriteLine("\nStrings are Equal");
            }
            else
            {
                Console.WriteLine("\nStrings are not Equal");
            }

            Console.ReadLine();
        }
    }
}

Output

Strings Compare without using String Functions

Thursday, September 26, 2013

Insert into Array

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

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

            int n, pos;

            Console.WriteLine("Enter the array total number of elements:\t");
            n = Convert.ToInt32(Console.ReadLine());

            int[] num = new int[100];

            Console.WriteLine("Enter the Array Element one by one");
            for (int i = 0; i < n; i++)
            {
                num[i] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("------------------------------");
            Console.WriteLine("         Array Elements       ");
            Console.WriteLine("------------------------------");
            Console.WriteLine();
            for (int i = 0; i < n; i++)
            {
                Console.Write(num[i] + "\t");
            }

            Console.WriteLine("\n\n");
            Console.WriteLine("***************************");
            Console.WriteLine("   Sorted Array Elements   ");
            Console.WriteLine("***************************");
            Console.WriteLine();
            for (int i = 0; i < n; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (num[i] > num[j])
                    {
                        int temp = num[i];
                        num[i] = num[j];
                        num[j] = temp;
                    }
                }
            }

            for (int i = 0; i < n; i++)
            {
                Console.Write(num[i] + "\t");
            }

            Console.WriteLine("\n");
            Console.Write("Enter the position to insert in array:\t");
            pos = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("\n");
            Console.Write("Enter the value to insert:\t");
            int val = Convert.ToInt32(Console.ReadLine());

            for (int c = n - 1; c >= pos - 1; c--)
            {
                num[c + 1] = num[c];
            }
            num[pos - 1] = val;

            Console.WriteLine("\n");
            for (int i = 0; i <= n; i++)
            {
                Console.Write(num[i] + "\t");
            }
            Console.ReadLine();
           
                     
        }
    }
}


Output

Insert element into Array using console Application

Reverse Number

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

namespace ReversNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int num,reverse=0;
            Console.WriteLine("Enter the number to reverse order");
            num = Convert.ToInt32(Console.ReadLine());

            while (num != 0)
            {
                reverse = reverse * 10;
                reverse = reverse + num % 10;
                num = num / 10;
            }
            Console.WriteLine("The Answer is:"+reverse);
            Console.ReadLine();
        }
    }
}

Output



Monday, September 9, 2013

Litterals

Litterals

A literal is a source code representation of a value.

Litterals Types 

  1. Boolean
  2. Integer
  3. Real
  4. Character
  5. String
  6. Null

Printing different types of Litterals

Program : 1

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

namespace LitteralProgram123
{
    class Program1
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
            Console.ReadKey();
        }        
    }
}

Output:





Sunday, September 8, 2013

C# Introduction

C# (C Sharp)

Definition - What does C# (C Sharp) mean?

C# is a general object-oriented programming (OOP) language for networking and Web development. C# is specified as a common language infrastructure (CLI) language. 
In January 1999, Dutch software engineer Anders Hejlsberg formed a team to develop C# as a complement to Microsoft’s NET framework. Initially, C# was developed as C-Like Object Oriented Language (Cool). The actual name was changed to avert potential trademark issues. In January 2000, NET was released as C#. Its NET framework promotes multiple Web technologies. 
The term is sometimes spelled as C Sharp or C-Sharp.

What is Console Application?


A console application is an application that runs in a console window same as a C and C++ program.

It doesn’t have any graphical user interface. Console Applications will have character based interface.  

Program : 1 "Add 2 Numbers"

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

namespace SimpleProject
{
     public class Add2Num
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Enter the 1st Number:\t");
            int no1 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter the 2nd Number:\t");
            int no2 = Convert.ToInt32(Console.ReadLine());

            int tot=no1+no2;
            Console.WriteLine("Total:="+tot);
            Console.ReadKey();
        }
    }
}

Output

Problem1O/P


Note  

To write C# console program , 
Open Visual Studio -> File -> New Project -> Visual C# -> select Console Applications