Sponsored Ad

Friday, September 24, 2010

While Loop Program Using C# | While Loop Example

This simple program demonstrate the use of while loop in C#. While loop run until condition reaches to false.

image

Code for While Loop:

using System;
using System.Text;
using System.Data;
namespace Console_App
{
    public class clsWhileLoop
    {
        public static void Main()
        {
            try
            {
                Console.WriteLine("Enter a number:");
                int GivenNumber = Convert.ToInt16(Console.ReadLine());
                while (GivenNumber < 10)
                {
                    Console.WriteLine("Loop Count {0} ", GivenNumber);
                    GivenNumber++;
                }
            }
            catch (Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();
        }
    }
}

How to use If Else in C# | If Else Example using C#

 

This is program of If Else complex and simple examples. This example shows the exact use of If Else statement in C#.

How to use If Else in C# | If Else Example using C#

Source Code of If Else Example:

using System;
using System.Text;
using System.Data;
namespace Console_App
{
    public class clsIfElase
    {
        public static void Main()
        {
            try
            {            
                int iInt;

                Console.Write("Enter a number for IF Else Example: ");            
                iInt = Int32.Parse(Console.ReadLine());
                if (iInt > 0)
                {
                    Console.WriteLine("{0} is greater than zero.", iInt);
                }

               if (iInt < 0)
                    Console.WriteLine("{0} is less than zero.", iInt);

                //simple example
                if (iInt != 0)
                {
                    Console.WriteLine("{0} is not equal to zero.", iInt);
                }
                else
                {
                    Console.WriteLine("{0} is equal to zero.", iInt);
                }
                //complex example
                if (iInt < 0 || iInt == 0)
                {
                    Console.WriteLine("{0} is less than or equal to zero.", iInt);
                }
                else if (iInt > 0 && iInt < 100)
                {
                    Console.WriteLine("{0} is between 1 and 100.", iInt);
                }
                else if (iInt > 101 && iInt < 200)
                {
                    Console.WriteLine("{0} is between 101 and 200.", iInt);
                }
                else if (iInt > 201 && iInt < 300)
                {
                    Console.WriteLine("{0} is between 201 and 300.", iInt);
                }
                else
                {
                    Console.WriteLine("{0} is greater than 300.", iInt);
                }
            }
            catch (Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();
        }
    }
}

How to use Array in C# | C# Array Examples

 

This is simple example to demonstrate the use of array. Please send us email or comment below if you feel any problem while using arrays.

How to use Array in C# | C# Array Examples

using System;
using System.Text;
using System.Data;
namespace Console_App
{
    public class clsArray
    {
        public static void Main()
        {
            try
            {
                int[] iInts = { 15, 20, 25 };
                bool[][] bBools = new bool[2][];
                bBools[0] = new bool[2];
                bBools[1] = new bool[1];
                double[,] dDoubles = new double[2, 2];
                string[] sStrings = new string[3];
                Console.WriteLine("Arrya Example");
                Console.WriteLine();

                Console.WriteLine("iInts[0]: {0}, iInts[1]: {1}, iInts[2]: {2}", iInts[0], iInts[1], iInts[2]);

                bBools[0][0] = false;
                bBools[0][1] = false;
                bBools[1][0] = true;
                Console.WriteLine("bBools[0][0]: {0}, bBools[1][0]: {1}", bBools[0][0], bBools[1][0]);

                dDoubles[0, 0] = 2.11;
                dDoubles[0, 1] = 1.830;
                dDoubles[1, 1] =11.111;
                dDoubles[1, 0] = 55.55667788;
                Console.WriteLine("dDoubles[0,0]: {0}, dDoubles[1,0]: {1}", dDoubles[0, 0], dDoubles[1, 0]);

                sStrings[0] = "Csharptalk.com";
                sStrings[1] = "Indihub.com";
                sStrings[2] = "bharatclick.com";
                Console.WriteLine("sStrings[0]: {0}, sStrings[1]: {1}, sStrings[2] {2}", sStrings[0], sStrings[1], sStrings[2]);
            }
            catch (Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();
        }
    }
}

Monday, September 20, 2010

Demo of Binary Operations in C#

Binary Operations are performed between two operands. here is few C# binary operation and their results.

Demo of Binary Operations in C#

Code:

using System;
using System.Text;
using System.Data;
namespace Console_App
{
    public class clsBinaryOperations
    {
        public static void Main()
        {
            try
            {
                int value1, value2, result;
                float floatResult;

                Console.WriteLine("Program for Binary Operations in C#");

                value1 = 5;
                value2 = 9;

                Console.WriteLine("Value of value1: {0}", value1);
                Console.WriteLine("Value of value2: {0}", value2);

                result = value1 + value2;
                Console.WriteLine("Result of value1+value2: {0}", result);

                result = value1 - value2;
                Console.WriteLine("Result of value1-value2: {0}", result);

                result = value1 * value2;
                Console.WriteLine("Result of value1*value2: {0}", result);

                result = value1 / value2;
                Console.WriteLine("Result of value1/value2: {0}", result);

                floatResult = (float)value1 / (float)value2;
                Console.WriteLine("Result of (float)value1 / (float)value2: {0}", floatResult);

                result = value1 % value2;
                Console.WriteLine("Result of  value1%value2: {0}", result);

                result += value1;
                Console.WriteLine("Result of  result+=value1: {0}", result);
            }
            catch (Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();
        }
    }
}

Friday, September 10, 2010

C# Program to Calculate Square Root of a Number

To calculate Square root of a number there is a inbuilt function inside the Math class. So you can directly use this function to find out the Square root of a number.

C# Program to Calculate Square Root of a Number

 

C# Program to Calculate Square Root of a Number

using System;
using System.Text;
using System.Collections;
using System.Data;
namespace Console_App
{
    public class clsFactorial
    {      
        public static void Main()
        {
            try
            {
                Console.WriteLine("Enter a number for Square Root:");
                 int Number =Convert.ToInt16( Console.ReadLine());
                 double SqrtNumber = Math.Sqrt(Number);

                 Console.WriteLine("Square root of Number {0} is: {1}", Number, SqrtNumber);                   
            }
            catch(Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();           
        }
    }
}

Sponsored Ad

Development Updates