Sponsored Ad

Thursday, August 19, 2010

Factorial Number Program using For Loop in C#

This program calculate factorial of given number using for loop in C#. for example this program calculate factorial of 4 and give answer 24. you can try this program for different numbers. instead of for loop you can also use other loops like while and do while loop or you can go for recursive functions.

Factorial Number Program using For Loop in C#

 

using System;
using System.Text;
using System.Collections;
using System.Data;
namespace Console_App
{
    public class clsFactorial
    {
        public static void Main()
        {
            try
            {
                Console.WriteLine("The factorial of 4 is: {0}\n",
                    Factorial(4));
            }
            catch(Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();           
        }

        static long Factorial(long number)
        {
            long FinalNumber = 1;
            for (int i = 1; i <= number; i++)
            {
                FinalNumber = FinalNumber * i;
            }
            return FinalNumber;
        }
    }
}

Factorial Number Program using C# Recursion Function

This is simple program to calculate factorial number of a given number using recursion functions. This program is full written in C# and supported by every version. You can also use loops instead of recursion functions to calculate factorial number.

Please feel free to contact us if you feel any problem while implementing recursion logic.

Factorial Number Program using C# Recursion Function

Source Code:

using System;
using System.Text;
using System.Collections;
using System.Data;
namespace Console_App
{
    public class clsFactorial
    {
        public static void Main()
        {
            try
            {
                Console.WriteLine("The factorial of 6 is: {0}\n",
                    Factorial(6));
            }
            catch(Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();           
        }

        static long Factorial(long number)
        {
            if (number <= 1)
                return 1;
            else
                return number * Factorial(number - 1);
        }  
    }
}

Wednesday, August 18, 2010

How to Handle string to date conversion error in C#

Sometime while coding, programmers forget to check for valid date type exception and they directly use the date string to convert it while there is ways by which you can check for valid date sting and avoid the unwanted exception.

1. Check for null string
2. Keep conversion logic in try catch block
3. if date variable is object then check for null also

How to Handle string to date conversion error in C#

 

using System;
using System.Text;
using System.Collections;
using System.Data;
namespace Console_App
{
    public class DateConversion
    {
        public static void Main()
        {
            string strDate1 = "", strDate2 = "10/10/2010";
            object obj1=null, obj2 = "10/10/2010";

            try
            {
                if(strDate1 != "")
                Console.WriteLine("string to DateConversion conversion: " + Convert.ToDateTime(strDate1));
            if (strDate2 != "")
                Console.WriteLine("string to DateConversion conversion: " + Convert.ToDateTime(strDate2));

            if (obj1 != null && obj1 != "")
                Console.WriteLine("string to DateConversion conversion: " + Convert.ToDateTime(obj1));
            if (obj2 != null && obj2 != "")
                Console.WriteLine("string to DateConversion conversion: " + Convert.ToDateTime(obj2));
            }
            catch(Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();           
        }
    }
}

How to Remove and Add DataTables from DataSet

This example demonstrate a simple C# code to add and remove datatable from dataset. In this example 3 datatables are added to dataset and then two datatables have been removed from dataset by using two different methods. the output of this program is table3.

How to Remove and Add DataTables from DataSet

 

using System;
using System.Text;
using System.Collections;
using System.Data;
namespace Console_App
{
    public class DeleteTables
    {
        public static void Main()
        {
            DataSet oDs = new DataSet();
            DataTable ot1 = new DataTable();
            DataTable ot2 = new DataTable();
            DataTable ot3 = new DataTable();

            //Add datatable into dataset
            oDs.Tables.Add(ot1);
            oDs.Tables.Add(ot2);
            oDs.Tables.Add(ot3);

            Console.WriteLine("Total tables in Dataset:");

            foreach (DataTable t in oDs.Tables)
                Console.WriteLine(t.TableName);

            oDs.Tables.Remove(ot2);
            oDs.Tables.RemoveAt(0);

            Console.WriteLine();
            Console.WriteLine("After removing tables in Dataset:");

            foreach (DataTable t in oDs.Tables)
                Console.WriteLine(t.TableName);

            Console.ReadLine();

        }
    }

}

Monday, August 16, 2010

How to Get Maximum of Numbers in JavaScript

Are you facing problem to get maximum number in JavaScript. here is very simple inbuilt function to get maximum of given numbers. the full code is given below to get the maximum of given numbers. This code check the maximum of 2,4,6,8 and gives the output 8.

Output:

How to Get Maximum of Numbers in JavaScript

Source Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

    <script type="text/javascript">
    document.write("Maximum of numbers:<br>" )
    document.write("2, 4, 6, 8:<br>");
    document.write("Result is: " + Math.max(2,4,6,8))
    </script>

</head>
<body>
</body>
</html>

How to Process Array in JavaScript

This is simple ASP.NET – JavaScript  article which will help you to navigate through the all elements of array. You can copy the code in below and run at your visual studio. Please let me know if you face any problem.

Output:

How to Process Array in JavaScript 

Source Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

    <script type="text/javascript">
var myList = new Array("http://IndiHub.com", "http://InterviewCity.com", "http://CsharpHub.com","http://SoftwareTestingNet.com")
for (i=0; i<myList.length; i++){
document.write(myList[i] + "<br>")
}
    </script>

</head>
<body>
</body>
</html>

Sponsored Ad

More Related Articles

Website Update

Followers