Your Ad Here

Friday, May 30, 2008

Display Current Data And Time

/* This program displays the words “Hello World!” on the screen,
along with the current date and time */

class Hello
{
public static void Main()
{
// display “Hello World!” on the screen
System.Console.WriteLine(“Hello World!”);
// display the current date and time
System.Console.WriteLine(“The current date and time is “ +
System.DateTime.Now);
}
}

Exception Handling

/* Simple Example On Exception Handling. This program is just intended to give you an idea of how an exception is handled by your C# program. */

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

namespace Program1
{
class Program
{
static void Main(string[] args)
{
double a;

try
{
Console.WriteLine(Enter A float Number);
a = Convert.ToDouble(Console.ReadLine());
}
catch (SystemException ex)
{
Console.WriteLine(ex);
}
}
}
}

Exception Handling - Addition Of Two Byte Numbers

/* C sharp program on exception handling. Addition of two byte numbers. */

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

namespace Program1
{
class check
{
public static void Main()
{
byte numOne;
byte numTwo;
byte result;

Console.WriteLine("Byte Size - (0-255)");
Console.Write("Enter a byte number : ");
numOne = Convert.ToByte(Console.ReadLine());
Console.Write("Enter a byte number : ");
numTwo = Convert.ToByte(Console.ReadLine());

try
{
checked
{
result = (byte) (numOne + numTwo);
}
Console.WriteLine("Result : " + result);
}

catch (ArithmeticException ex)
{
Console.WriteLine(ex);
}
}
}
}

Saturday, May 24, 2008

Negate the positive Elements of The Array

// C# program to Negate the positive Elements of The Array

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

namespace Step2
{
class negate
{
public static void Main()
{
int[] a = new int[10];
for (int i = 0; i < 10; i++)
{
a[i] = Convert.ToInt16(Console.ReadLine());
if (a[i] > 0)
a[i] = -a[i];
}
Console.WriteLine("Elements:");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(a[i]);
}
}
}
}

Grestest Of Three Numbers

// Greater Of Three Numbers

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

namespace Step2
{
class Program
{
static void Main(string[] args)
{
long a, b,c;
start:
try
{

Console.WriteLine("Enter Three Nos");
a = Convert.ToInt64(Console.ReadLine());
b = Convert.ToInt64(Console.ReadLine());
c = Convert.ToInt64(Console.ReadLine());

if (a > b && a > c)
Console.WriteLine("{0} is greater ", a);
else if (b > c && b > a)
Console.WriteLine("{0} is greater", b);
else
Console.WriteLine("{0} is Greater", c);
}

catch (Exception e)
{
Console.WriteLine(e.Message);
goto start;
}
}
}
}

Division Of Two Numbers

/*** C# program to divide two numbers **/

using System;

namespace Program1
{
class except
{
static void Main(string[] args)
{
double a,b,ans;

Console.WriteLine("Enter A Number");
a = Convert.ToDouble(Console.ReadLine());

Console.WriteLine("Enter Another Number");
b = Convert.ToDouble(Console.ReadLine());

ans = a / b;

Console.WriteLine("Answer : " + ans);
}
}
}

Finding Simple Interest

using System;
namespace Interest
{
class Program
{
static void Main(string[] args)
{
int year;
double princ_amt,rate, interest, total_amt;
Console.Write("Enter The Principle Amount : ");
princ_amt = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter The Term : ");
year = Convert.ToInt16(Console.ReadLine());

Console.Write("Enter The Rate Of interest : ");
rate = Convert.ToDouble(Console.ReadLine());

interest = princ_amt * year * rate / 100;
total_amt = princ_amt + interest;

Console.WriteLine("\n----------------------------------------");
Console.WriteLine("Interest : {0} Total Amount : {1}", interest, total_amt);
Console.WriteLine("----------------------------------------");

}
}
}

Friday, May 23, 2008

First Program

using System;

namespace MyFirstPrograms
{
class Program
{
static void Main(string[] args)
{
string name;
Console.Write("What is your name: ");
name = System.Console.ReadLine();

Console.WriteLine("Welcome "+name+" to Your First C# Program!");
}
}
}