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

Wednesday, June 30, 2010

Check the entered year is Leap year or not in c# console

open a console application project in c# language in your editor and just paste the following code in your class's brackets -

static void Main(string[] args)
{
try
{
Console.Write("Please Enter the Year: ");
int year = int.Parse(Console.ReadLine());
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
{ Console.WriteLine("The year {0}, is a Leap Year", year);
}
else
{
Console.WriteLine("The year {0}, is not a Leap Year", year);
} }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine(); }

Output is-

Create a Pyramid in C# Console

open a console application project in c# language in your editor and just paste the following code in your class's brackets -

static void Main(string[] args)
{

for (int i = 0; i < k =" i;" j =" 2"> 0; j--)
{
Console.Write("*"); }
Console.WriteLine("");
}
Console.ReadLine();
}

Output is-

Friday, March 26, 2010

Get L.C.M (Least Common Multiple) of two numbers in C#

open a console application project in c# language in your editor and just paste the following code in your class's brackets -


static void Main(string[] args) {

int tempNum, number1, number2;
Console.Write("Enter First Number: ");
number1 = int.Parse(Console.ReadLine());
Console.Write("Enter Secnd Number: "); number2 = int.Parse(Console.ReadLine());
Console.WriteLine("\n");
for (tempNum = 1; ; tempNum++)
{
if (tempNum % number1 == 0 && tempNum % number2 == 0)
{
Console.WriteLine("L.C.M is - "); Console.WriteLine(tempNum.ToString());
Console.Read();
break;
}
}
}

Output is -

How to generate Fibonacci series in c#

open a console application project in c# language in your editor and just paste the following code in your class's brackets -

static void Main(string[] args)
{
int previous = -1; int next = 1;
int position;
Console.WriteLine("Enter the position");
position = int.Parse(Console.ReadLine());
for (int i = 0; i < position; i++)
{
int sum = next + previous; previous = next;
next = sum;
Console.WriteLine(next);
}
Console.ReadLine();
}

Output is -

Reverse a String in C#

open a console application project in c# language in your editor and just paste the following code in your class's brackets -

static void Main(string[] args)
{
Console.Write("Enter String: "); String temp = Console.ReadLine();
Console.WriteLine("\n");
int len = temp.Length;
char[] arr = new char[len];

for (int i = 0; i < len; i++)
{ arr[i] = temp[len - 1 - i];
}
Console.WriteLine(arr);
Console.Read();
}

Output is -