You are on page 1of 23

//Factorial

using S=System.Console;
using System;
class fact
{
public static void Main()
{
int n,fact=1,i;
S.WriteLine("Enter the value");
n=int.Parse(S.ReadLine());
for(i=1;i<=n;i++)
{
fact=fact*i;
}
S.WriteLine("the factorial is:"+fact);
}
}
OUTPUT:
Enter the value
5
the factorial is:120
// String Reverse

using S=System.Console;
using System;
class rev
{
public static void Main()
{
string str;
S.WriteLine("\n Enter the input string to be reversed:");
str=S.ReadLine();
char[]c=str.ToCharArray();
Array.Reverse(c);
S.Write("\n The reversed string is:");
foreach(char n in c)
S.Write(n);
S.Write("\n");
}
}

OUTPUT:

Enter the input string to be reversed:


welcome
The reversed string is: emoclew
//Prime or Not

using System;
class prime
{
public static void Main()
{
int c=0,i;
Console.WriteLine("Enter a number to calculate prime number:");
int number=Convert.ToInt16(Console.ReadLine());
for(i=2;i<=number;i++)
{
if(number%i==0)
c++;
}
if(c==1)
Console.WriteLine("prime");
else
Console.WriteLine("not prime");
}
}

OUTPUT:
Enter a number to calculate prime number:
7
prime
//Fibonacci Series

using System;
class fibo1
{
public static void Main()
{
int a=0,b=1,c,d;
Console.WriteLine("Enter the no.of series:");
c=int.Parse(Console.ReadLine());
Console.WriteLine("fibo series is:");
Console.WriteLine(a);
Console.WriteLine(b);
for(int i=2;i<c;i++)
{
d=a+b;
Console.WriteLine(d);
a=b;
b=d;
}
}
}

OUTPUT:
Enter the no.of series:
6
fibo series is:
0
1
1
2
3
5
// Command Line Arguments

using System;
class arg
{
public static void Main(string [] a)
{
foreach(string i in a)
Console.WriteLine(" Your string is : "+i);
}
}

OUTPUT:
Your string is : hello
Your string is : welcome
Your string is : to
Your string is : all
//Jumps in Loop

using System;
class br
{
public static void Main()
{
for(int i=1;i<100;i++)
{
Console.WriteLine(" ");
if (i >= 10)
break;
for(int j=1;j<100;j++)
{
Console.Write("*");
if(j==i)
goto loop1;
}
loop1: continue;
}
Console.Write("Termination by break");
}
}
OUTPUT:
*
**
***
****
*****
******
*******
********
*********
//Boxing and UnBoxing

using System;
class box
{
public static void Main()
{
int s;
object obj;
s=26;
obj=s;
Console.WriteLine("\n ...Boxing.......");
Console.WriteLine("integers variable containts:"+s);
Console.WriteLine("integer box 'obj'containts:"+obj);
int x;
x=(int)obj;
Console.WriteLine("\n.....Unboxing....");
Console.WriteLine("integer box'obj'containts:"+obj);
Console.WriteLine("integer box after unboxing to integer 'x' and so x
containts:"+x);
}
}

OUTPUT:

...Boxing.......
integers variable containts:26
integer box 'obj'containts:26

.....Unboxing....
integer box'obj'containts:26
integer box after unboxing to integer 'x' and so x containts:26
// Parameter Array
using System;
class param
{

static void parray(params int [] arr)


{
Console.WriteLine("array elements are : ");
foreach(int i in arr)
Console.Write(" "+i);
Console.WriteLine();
}

public static void Main()


{
int [ ] x={11,22,33};
parray(x);
parray();
parray(100,200);
}
}

OUTPUT:
array elements are : 11 22 33
array elements are :
array elements are : 100 200
// Constructor
using System;
class cons
{
static int i;
static cons()
{
i=4;
Console.WriteLine("Inside static constructor.....");
}
public cons()
{
Console.WriteLine("Inside regular constructor..i={0}",i);
}
~cons()
{
Console.WriteLine("Inside destructor");
}
static void Main()
{
Console.WriteLine("cons");
cons c1=new cons();
cons c2=new cons();
Console.ReadLine();
}
}
OUTPUT:
Inside static constructor.....
cons
Inside regular constructor..i=4
Inside regular constructor..i=4
Inside destructor
Inside destructor
//Pass by reference and output parameter

using System;

class RO
{
static void swap(ref int x, ref int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
static void square (int x, out int z)
{
z=x*x;
}
public static void Main()
{
int m=100;
int n=200;
Console.WriteLine("Actual Value \n");
Console.WriteLine("m="+m);
Console.WriteLine("n="+n);
Console.WriteLine("\n Pass by Reference\n");
swap(ref m,ref n);
Console.WriteLine("m="+m);
Console.WriteLine("n="+n);
Console.WriteLine("\n output parameter \n");
int p;
square(m,out p);
Console.WriteLine("p="+p);
}
}
OUTPUT:

Actual Value
m=100
n=200

Pass by Reference
m=200
n=100

output parameter
p=40000
//Array methods

using System;
class sort
{
public static void Main()
{
int[]x={30,10,80,90,20};
Console.WriteLine("\n array before sorting \n");
foreach(int i in x)
Console.Write(" "+i);
Array.Sort(x);

Console.WriteLine("\n array after sorting \n");


foreach(int i in x)
Console.Write(" "+i);

Console.WriteLine("\n\n array after reversing \n");


Array.Reverse(x);
foreach(int i in x)
Console.Write(" "+i);

Console.WriteLine("\n\n array length:");


Console.WriteLine(x.Length);

Console.WriteLine("\n\n deleting an item from array:");


Array.Clear(x,3,1);
foreach(int i in x)
Console.Write(" "+i);

Console.WriteLine("\n\n copying the items from one array to another:");


int[]y=new int[x.Length];
x.CopyTo(y,0);
foreach(int i in x)
Console.Write(" "+i);
Console.WriteLine("\n\n displaying an item using getvalue function:");
for(int i=0;i<x.Length;i++)
{
Console.WriteLine(x.GetValue(i)+" ");
}
Console.WriteLine();
}
}

OUTPUT:

Array before sorting


30 10 80 90 20

Array after sorting


10 20 30 80 90

Array after reversing


90 80 30 20 10

Array Length : 5

Deleting an item from array :


90 80 30 0 10

Copying the items from one array to another:


90 80 30 0 10

displaying an item using getvalue function:


90 80 30 0 10
//String Methods

using System;
class sm
{
public static void Main()
{
string s1,s2,s3;
int c;
Console.WriteLine("Enter String S1 and S2\n");
s1=Console.ReadLine();
s2=Console.ReadLine();
Console.WriteLine("1.String Compare\n");
Console.WriteLine("2.String Insert\n");
Console.WriteLine("3.Remove\n");
Console.WriteLine("Enter Choice \n");
c=int.Parse(Console.ReadLine());

switch(c)
{

case 1:

bool n =string.Equals(s1,s2);
if (n==true)
Console.WriteLine("Strings are Equal \n");
else
Console.WriteLine("Strings are not Equal \n");
break;

case 2:

Console.WriteLine("Enter the Position \n");


int p=int.Parse(Console.ReadLine());
Console.WriteLine("Enter Substring \n");
s3=Console.ReadLine();
Console.WriteLine(s2.Insert(p,s3));
break;

case 3:

Console.WriteLine("Enter the Position \n");


int r=int.Parse(Console.ReadLine());
Console.WriteLine(s2.Remove(r,2));
break;
}
}
}
OUTPUT:

Enter String S1 and S2


hello
all

1.String Compare
2.String Insert
3.Remove

Enter Choice
2

Enter the Position


3

Enter Substring
welcome

allwelcome
//String Builder Methods

using System.Text;
using System;
class im
{
public static void Main()
{
StringBuilder s=new StringBuilder("Object ");
Console.WriteLine("Your String is :"+s);

Console.WriteLine("Length : "+s.Length);

s.Append(" Language ");


Console.WriteLine("Now String is :"+s);

s.Insert(7, "oriented");
Console.WriteLine("Modified String is :"+s);

int n= s.Length;
s[n-1]='!';
Console.WriteLine("Final String is : " +s);
}
}
OUTPUT:

Your String is :Object

Length : 7

Now String is :Object Language

Modified String is :Object oriented Language

Final String is : Object oriented Language!


//Single Inheritance

using System;

class Room
{
public int length;
public int breadth;

public Room(int x,int y)


{
length=x;
breadth=y;
}

public int Area()


{
return(length*breadth);
}
}

class BedRoom:Room
{
int height;

public BedRoom(int x,int y,int z):base(x,y)


{
height=z;
}

public int Volume()


{
return(length*breadth*height);
}
}
class InherTest
{
public static void Main()
{
BedRoom room1=new BedRoom(14,12,10);
int areal=room1.Area();
int volume1=room1.Volume();
Console.WriteLine("Area1= "+areal);
Console.WriteLine("Volume1= "+volume1);
}
}

OUTPUT:

Area1= 168

Volume1= 1680
//Interface
using System;
interface Area
{
double Compute(double x);
}
class Square:Area
{
public double Compute(double x)
{
return(x*x);
}
}
class Circle:Area
{
public double Compute(double x)
{
return(3.14*x*x);
}
}
class InterfaceTest2
{
public static void Main()
{
Square sqr=new Square();
Circle cir=new Circle();
Area area;
area=sqr as Area;
Console.WriteLine("Area of Square="+area.Compute(10.0));
area=cir as Area;
Console.WriteLine("Area of Circle="+area.Compute(10.0));
}
}
OUTPUT: Area of Square=100
Area of Circle=314
//Overloading
using System;
class overload
{
public static void Main()
{
Console.WriteLine(volume(10));
Console.WriteLine(volume(12.0f,8));
Console.WriteLine(volume(11,75,15));
}

static int volume(int x)


{
return(x*x*x);
}

static int volume(int l, int b, int h)


{
return(l*b*h);
}

static double volume(float r, int h)


{
return(3.14*r*r*h);
}
}

OUTPUT:

1000
3617.28
12375
// Overriding

using System;
public class Customer
{
public virtual void CustomerType()
{
Console.WriteLine("I am Customer");
}
}

public class CorporateCustomer:Customer


{
public override void CustomerType()
{
Console.WriteLine("I am corporate Customer");
}
}

public class PersonalCustomer:Customer


{
public override void CustomerType()
{
Console.WriteLine("I am personal Customer");
}
}

class demo
{
static void Main(string[] args)
{
Customer[]C=new Customer[3];
C[0]=new PersonalCustomer();
C[1]=new CorporateCustomer();
C[2]=new Customer();
foreach(Customer CustomerObject in C)
{
CustomerObject.CustomerType();
}
Console.ReadLine();
}
}

OUTPUT:
I am personal Customer
I am corporate Customer
I am Customer
//Operator Overloading

using S=System.Console;
using System;

class over
{
public int value;
public over(int x)
{
value=x;
}

public static over operator+(over a,over b)


{
return new over(a.value+b.value);
}
public void display()
{
S.WriteLine(value);
}
}
class mains
{
public static void Main()
{
over s1=new over(10);
over s2=new over(20);
over s=s1+s2;
S.Write("result:");
s.display();
}
}
OUTPUT:
result:30

You might also like