Tuesday, July 25, 2006

Factorial of a number using C #

Finding the factorial of a number in C#
the code is meant for finding the factorial both recursive and non recursive

public static int Factorial(int n)
{
int FactorialValue = 1;
for (int i = n; i > 0; i--)
{
FactorialValue = FactorialValue * i;
}
return FactorialValue;
}
public static int recursiveFactorial(int n)
{
if (n > 1)
{
return n * recursiveFactorial(n - 1);
}
else
{
return 1;
}
}
public static int recursiveFactorialIntermediate(int n, int[] nArray)
{
if (n > 1)
{
nArray[n - 1] = n * recursiveFactorialIntermediate(n - 1, nArray);
return nArray[n - 1];
}
else
{
return 1;
}
}

Sunday, June 25, 2006

largest element in the array by comparing to All - C#

To find out the largest element in the array by comparing to All elements method
This methd has an Big Oh notation performance of O(n2)


public static int GetIndexLargestCompareToAll(int[] numbers)
{
int currentMaxIndex = -1;
int length = numbers.Length;
bool isMax = false;

try
{
if (length == 0)
currentMaxIndex = -1;
else if (length == 1)
currentMaxIndex = 0;
else
{
for (int i = 0; i < length; i++)
{
isMax = true;
for (int j = 0; j < length; j++)
{
if (numbers[j] > numbers[i])
isMax = false;
}
if (isMax)
currentMaxIndex = i;
}
}

}
catch (Exception)
{
currentMaxIndex = -1;
}
return currentMaxIndex;
}

Largest Element in the Array using C #

To find out the largest element in the array by comparing to the Max method
This methd has an Big Oh notation performance of O(n)


public static int GetIndexLargestCompareToMax(int[] numbers)
{
int currentMaxIndex = -1;
int length = numbers.Length;

try
{
if (length == 0)
currentMaxIndex = -1;
else if (length == 1)
currentMaxIndex = 0;
else
{
currentMaxIndex = 0;
for (int j = 1; j < length; j++)
{
if (numbers[j] > numbers[currentMaxIndex])
{
currentMaxIndex = j;
}
}
}

}
catch (Exception)
{
currentMaxIndex = -1;
}
return currentMaxIndex;
}

Thursday, May 25, 2006

Calendar Object in C Sharp.NET

A friend of mine asked me to create a calendar object in C Sharp. I thought it might be useful to others too

So here is a one for you



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

namespace Algorithms
{
public class Calendar
{
private DateTime currentDate;
private int currentDay;
private int currentDayWeek;
private int currentMonth;
private int currentYear;
private bool leapYear;

public Calendar()
{
DateTime cDate = DateTime.Now;
currentDate = cDate;
currentDay = cDate.Day;
currentDayWeek = Convert.ToInt32(cDate.DayOfWeek);
currentMonth = cDate.Month;
currentYear = cDate.Year;
}
public Calendar(DateTime cDate)
{
currentDate = cDate;
currentDay = cDate.Day;
currentDayWeek = Convert.ToInt32(cDate.DayOfWeek);
currentMonth = cDate.Month;
currentYear = cDate.Year;

if (currentYear % 400==0)
leapYear = true;
else if (currentYear % 100==0)
leapYear = false;
else if (currentYear % 4==0)
leapYear = true;
else
leapYear = false;

}
public DateTime CurrentDate
{
get { return currentDate; }
set { currentDate = value;}
}
public int CurrentDay
{
get { return currentDay; }
set { currentDay = value; }
}
public int CurrentDayWeek
{
get { return currentDayWeek; }
set { currentDayWeek = value; }
}
public int CurrentMonth
{
get { return currentMonth; }
set { currentMonth = value; }
}
public int CurrentYear
{
get { return currentYear; }
set { currentYear= value; }
}
public bool LeapYear
{
get { return leapYear; }
}

public void Display()
{
Console.Write("== Calendar ==\n");

DateTime firstDay = currentDate.AddDays(1 - currentDay);
string dayDisplay ="";

if (Convert.ToInt32(firstDay.DayOfWeek) == 0)
{
dayDisplay = "Mo Tu We Th Fr Sa Su";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 1)
{
dayDisplay = "Tu We Th Fr Sa Su Mo";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 2)
{
dayDisplay = "We Th Fr Sa Su Mo Tu";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 3)
{
dayDisplay = "Th Fr Sa Su Mo Tu We";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 4)
{
dayDisplay = "Fr Sa Su Mo Tu We Th";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 5)
{
dayDisplay = "Sa Su Mo Tu We Th Fr";
}
else if (Convert.ToInt32(firstDay.DayOfWeek) == 6)
{
dayDisplay = "Su Mo Tu We Th Fr Sa";
}

Console.Write(dayDisplay + "\n");

// Common content
Console.Write("01 02 03 04 05 06 07\n");
Console.Write("08 09 10 11 12 13 14\n");
Console.Write("15 16 17 18 19 20 21\n");
Console.Write("22 23 24 25 26 27 28\n");

//Variable Date

// Feb 2
if (currentMonth == 2)
{
if (leapYear)// Feb 2 leap year
Console.Write("29 00 00 00 00 00 00\n");
else
Console.Write("00 00 00 00 00 00 00\n");
}

if ((currentMonth == 4) || (currentMonth == 6) || (currentMonth == 9) || (currentMonth == 11))
{
// 4, 6, 9, 11
Console.Write("29 30 00 00 00 00 00\n");
}
if ((currentMonth == 1) || (currentMonth == 3) || (currentMonth == 5) || (currentMonth == 7) || (currentMonth == 8) || (currentMonth == 10) || (currentMonth == 12))
{
// 1,3,5,7,8,10,12
Console.Write("29 30 31 00 00 00 00\n");
}



}

}
}

Thursday, April 27, 2006

LEARN Your Way to MCAD Excellence

If you want to learn all about the .Net Windows, Web, and Xml Web Services and also get an MCAD certification as bonus.

The following tips have helped me and 10's of my friends.

I will recommend THESE and ONLY THESE books in the FOLLOWING ORDER

The ORDER of the exam is also VERY IMPORTANT and it should be

1. Windows
2. Web
3. XML Web services

For each exam FIRST you must study completely the MS Press book (100%) and THEN AND ONLY THEN study Amit Kalani's one.

All the best !"

Amazon Link LEARN Your Way to MCAD Excellence

Tuesday, April 25, 2006

C Sharp Tricks - First Post

In this website you will find tips, tricks, gotchas and some recipes for windows as well as web applications. This site is intended to share some of the things I've developed, some source code, articles and some samples I've wrote. Please leave your comments to improve. Thanks and I hope you will find them as useful as i do - Rajesh Lal.

Along with C sharp you will also find the way it interacts with the related technologies.


  • Windows Vista

  • ASP.Net

  • Microsoft SQL Server

  • Javascript, AJAX, Atlas

  • Web 2.0 , CSS

  • Installation / deployment / orca

  • Mobile devices etc

  • C# foundation - data structures etc