C# Program
C# Program
namespace ConsoleApplication1
{
class bill
{
int pmr, imr, cons, n = 0;
double awt;
public void GetData()
{
Console.WriteLine("Enter Previous Meter Reading");
pmr = int.Parse(Console.ReadLine());
Console.WriteLine("Enter Later Meter Reading");
imr = int.Parse(Console.ReadLine());
cons = imr - pmr;
}
public void Calc()
{
if (cons != 0)
n = (cons - 1) / 100 + 1;
switch (n)
{
case 0: awt = 40.0; break;
case 1: awt = 10.0 + cons * 1.5; break;
case 2: awt = 20.0 + cons + 1.5; break;
case 3: awt = 430.0 + (cons - 200) * 3; break;
default: awt = 1840.0 + (cons - 500) * 5.75; break;
}
}
public void show()
{
Console.WriteLine("Previous Meter Reading...{0}", pmr);
Console.WriteLine("Latest Meter Reading...{0}", imr);
Console.WriteLine("Unit Consumption...{0}", cons);
Console.WriteLine("Amount To Paid...{0}", Math.Ceiling(awt));
}
}
class EB
{
public static void Main(string[] args)
{
bill z = new bill();
z.GetData();
z.Calc();
z.show();
Console.ReadKey();
}
}
}
Output :
2. Prime Number Between Two Given Numbers
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
char[] a = { 'H', 'E', 'L', 'L', 'L', 'O' };
foreach (char ch in a)
{
Console.WriteLine(ch);
}
Console.ReadKey();
}
}
}
Output :
4. Find ‘n’ Factorial Using Recursion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number : ");
int a = int.Parse(Console.ReadLine());
long fact = factcalc(a);
Console.WriteLine(" The factorial of {0} is {1}", a, fact);
Console.ReadKey();
}
private static long factcalc (int a)
{
if (a == 0)
{
return 1;
}
return a * factcalc(a - 1);
}
}
}
Output :
5. Constructor Overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Area
{
public Area(int a)
{
Console.WriteLine(" Area of square = " + a * a);
}
public Area(float r)
{
Console.WriteLine(" Area of circle = " + 3.14 * r * r);
}
public Area(double s)
{
Console.WriteLine(" Volume of a cube = " + s * s * s);
}
public Area(double l, double b)
{
Console.WriteLine(" Area of rectangle = " + l * b);
}
class program
{
public static void Main(string[] args)
{
Area z1 = new Area(6);
Area z2 = new Area(10.0f);
Area z3 = new Area(6.0);
Area z4 = new Area(20.0, 15.0);
Console.ReadKey();
}
}
}
}
Output :
6. Sort ‘n’ Number Using Method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public void get()
{
List<int> names = new List<int>();
Console.WriteLine("How many numbers :");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the values :");
for (int i = 0; i < a; i++)
{
int j = int.Parse(Console.ReadLine());
names.Add(j);
}
names.Sort();
Console.WriteLine("\n After sorting :\n");
var asn = from s in names orderby a ascending select s;
foreach (int b in asn)
{
Console.WriteLine(b);
Console.ReadKey();
}
}
}
class Sort
{
static void Main(string[] args)
{
Program p = new Program();
p.get();
Console.ReadKey();
}
}
}
Output :
7. Perform Matrix Operations Using Object
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class MatrixTranspose
{
int m, n, i, j;
int[,] a = new int[10, 10];
public void get()
{
Console.WriteLine(" Enter the order of the matrix :");
m = int.Parse(Console.ReadLine());
n = int.Parse(Console.ReadLine());
}
}
public void Transpose()
{
Console.WriteLine("Transpose matrix :");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
Console.WriteLine(a[j, i]);
}
Console.WriteLine();
Console.ReadKey();
}
}
}
class Trans
{
static void Main(string[] args)
{
MatrixTranspose m = new MatrixTranspose();
m.get();
m.display();
m.Transpose();
Console.ReadKey();
}
}
}
Output :
8. Implement User Defined Exception
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class myException : Exception
{
public myException(String message):base(message)
{
}
}
class testmyException
{
static void Main(string[] args)
{
int mark1,mark2,mark3;
Console.WriteLine("enter mark1,mark2,mark3:");
mark1=Int32.Parse(Console.ReadLine());
mark2=Int32.Parse(Console.ReadLine());
mark3=Int32.Parse(Console.ReadLine());
try
{
if((mark1>100) || (mark2>100) || (mark3>100))
{
throw new myException("Mark out of Range:");
}
}
catch(myException e)
{
Console.WriteLine("caught Exception");
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("BYE");
Console.ReadLine();
}
Console.ReadKey();
}
}
}
Output :
9. Sort Names Using Array-List Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
List<string> abs = new List<string>();
Console.WriteLine("How many names");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the names one by one");
for (int i = 1; i <= a; i++)
{
String j = Console.ReadLine();
abs.Add(j);
}
abs.Sort();
Console.WriteLine("\n After sorting:");
foreach (string s in abs)
Console.WriteLine(s);
Console.ReadLine();
}
}
}
Output :
10. Implement Inheritance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class a
{
protected string name;
public void Name()
{
Console.WriteLine("Enter your name :");
name = Console.ReadLine();
}
}
class b : a
{
protected string regno;
public void Regno ()
{
Console.WriteLine("Enter your regno :");
regno = Console.ReadLine();
}
}
class c : b
{
protected string major;
public void Major()
{
Console.WriteLine("Enter your major :");
major = Console.ReadLine();
}
}
class d : c
{
protected string college;
public void College()
{
Console.WriteLine("Enter your college :");
college = Console.ReadLine();
}
public void show()
{
Console.WriteLine("\n\n DATA ENTERED :\n\n");
Console.WriteLine(name);
Console.WriteLine(regno);
Console.WriteLine(major);
Console.WriteLine(college);
}
}
class program
{
public static void Main(string[] args)
{
d z = new d();
z.Name();
z.Regno();
z.Major();
z.College();
z.show();
Console.ReadKey();
}
}
}
Output :
11. Implement Operator Overloading
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace overloading
{
class Calculate
{
public int n1, n2;
public Calculate(int no1, int no2)
{
n1 = no1;
n2 = no2;
}
public static Calculate operator + (Calculate c1)
{
c1.n1 = +c2.n1;
c1.n2 = +c2.n2;
return c1;
}
public void print()
{
Console.WriteLine(" Number 1 :"+n1);
Console.WriteLine(" Number 2 :"+n2);
Console.ReadKey();
}
}
class over
{
static void Main(string[]) args;
{
Calculate c =new Calculate(90,40);
c = + c;
c.print();
Console.ReadKey();
}
}
}
Output :
12. Implement Polymorphism
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Shape
{
protected int width, height;
public Shape(int a, int b)
{
width = a;
height = b;
}
public virtual int area()
{
Console.WriteLine("parent Class");
return 0;
}
}
class Rectangle:Shape
{
public Rectangle(int a,int b): base(a,b)
{
}
public override int area()
{
Console.WriteLine("Rectangle class area:");
return(width*height);
}
}
class Triangle:Shape
{
public Triangle(int a,int b):base(a,b)
{
}
public override int area()
{
Console.WriteLine("Triangle class area:");
return(width*height/2);
}
}
class Caller
{
public void CallArea(Shape s)
{
int a;
a=s.area();
Console.WriteLine("Area {0}",a);
}
}
class DyPoly
{
static void Main(string[] args)
{
Caller c=new Caller();
Rectangle r=new Rectangle(10,7);
Triangle t=new Triangle(10,5);
c.CallArea(r);
c.CallArea(t);
Console.ReadKey();
}
}
}
Output :
13. Implement Interfaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace application1
{
class program
{
public interface drawable
{
void draw();
}
public class rectangle : drawable
{
public void draw()
{
Console.WriteLine("Drawing rectangle...");
}
}
public class circle : drawable
{
public void draw()
{
Console.WriteLine("Drawing circle...");
}
}
public class Testinterface
{
public static void Main()
{
drawable d;
d = new rectangle();
d.draw();
d = new circle();
d.draw();
Console.ReadKey();
}
}
}
}
Output :
14(a) . Implement Overriding Methods
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class A1
{
public virtual void abc()
{
Console.WriteLine("In A");
}
}
class B1 : A1
{
public override void abc()
{
Console.WriteLine("In B");
}
}
class C1 : B1
{
public override void abc()
{
Console.WriteLine("In C");
}
}
class Ride
{
static void Main(string[] args)
{
A1 z1 = new A1();
B1 z2 = new B1();
C1 z3 = new C1();
z1.abc();
z2.abc();
z3.abc();
Console.ReadKey();
}
}
}
Output :
14(b) . Implement Hiding Methods
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class A1
{
public new void abc()
{
Console.WriteLine("In A");
}
}
class B1 : A1
{
public new void abc()
{
Console.WriteLine("In B");
}
}
class C1 : B1
{
public new void abc()
{
Console.WriteLine("In C");
}
}
class ride
{
static void Main(string[] args)
{
A1 z1 = new A1();
B1 z2 = new B1();
C1 z3 = new C1();
z1.abc();
z2.abc();
z3.abc();
Console.ReadKey();
}
}
}
Output :
15.Copy Contents File
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace file
class Program
File.Copy("E:\\sam.txt", "E:\\sam3.txt");
File.Copy("E:\\sam3.txt", "E:\\sam4.txt");
Console.WriteLine(File.ReadAllText("E:\\sam.txt"));
Console.WriteLine(File.ReadAllText("E:\\sam3.txt"));
Console.WriteLine(File.ReadAllText("E:\\sam4.txt"));
Console.ReadKey();
}
Output: