0% found this document useful (0 votes)
69 views32 pages

C Sharp-1 New e

1. The document contains 9 C# code examples demonstrating different programming concepts: 2. The examples include preparing an electricity bill using a switch statement, finding prime numbers between two numbers, using a foreach loop, calculating factorials recursively, constructor overloading, matrix operations using objects, implementing user-defined exceptions, inheritance, and operator overloading. 3. Each code example contains the necessary using statements, classes, and methods to demonstrate the given concept.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views32 pages

C Sharp-1 New e

1. The document contains 9 C# code examples demonstrating different programming concepts: 2. The examples include preparing an electricity bill using a switch statement, finding prime numbers between two numbers, using a foreach loop, calculating factorials recursively, constructor overloading, matrix operations using objects, implementing user-defined exceptions, inheritance, and operator overloading. 3. Each code example contains the necessary using statements, classes, and methods to demonstrate the given concept.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

C# PROGRAMING

1.Prepare Electricity Bill using Switch Statement


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 latest 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 aid....{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 Number

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int n, i, x, a, b;
Console.WriteLine("Starting Number:");
a = int.Parse(Console.ReadLine());
Console.WriteLine("End Number:");
b = int.Parse(Console.ReadLine());
Console.WriteLine("The Prime Number Are:");
for (n = a; n <= b; n++)
{
x = 0;
for (i = 2; i <= n / 2; i++)
{
if (n % i == 0)
{
x++;
break;
}
}
if (x == 0 && n != 1)
{
Console.Write("{0}\n", n);
}
Console.ReadKey();
}
}
}
}
Output:
3.Program Using For Each Statement

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

namespace ConsoleApplication2
{
class ForEachLoop
{
public static void Main(string[] args)
{
char[] gender={'m','f','m','m','m','f','f','m','m','f'};
int male = 0, female = 0;
foreach (char g in gender)
{
if (g == 'm')
male++;
else if (g =='f')
female++;
}
Console.WriteLine("Number of male = {0}", male);
Console.WriteLine("Number of female = {0}", female);
}
}
}
Output:
4.Find n Factorial Using Recursion

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

namespace ConsoleApplication2
{
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 ConsoleApplication2
{
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.Perform Matrix Operations Using Object

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

namespace ConsoleApplication6
{
class MatrixTranspose
{
public
int row, col;
int[,] a = new int[10, 10];

public void get()


{
Int i, j;
Console.WriteLine("Enter the order of the Matrix: ");
row = int.Parse(Console.ReadLine());
col = int.Parse(Console.ReadLine());
Console.WriteLine("Enter the matrix element:");
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
a[i,j] = int.Parse(Console.ReadLine());
}
}
}

public void display()


{
Console.WriteLine("Given Matrix :");
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
Console.Write(a[i,j]+ ”\t”);
}
Console.WriteLine();
}
}
public void transpose()
{
Console.WriteLine("Transpose Matrix:");
for (int i = 0; i < col; i++)
{
for (int j = 0; j < row; j++)
{
Console.Write(a[j,i]+ “\t”);
}
Console.WriteLine();
}
}
}

class trans
{
public static void Main(string[] args)
{
MatrixTranspose m = new MatrixTranspose();
m.get();
m.display();
m.transpose();
Console.ReadKey();
}
}
}
Output:
7.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:
8.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:
9.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,Calculate c2)


{
c1.n1+=c2.n1;
c1.n2+=c2.n2;
return c1;
}
public void print()
{
Console.WriteLine("Number 1: " +n1);
Console.WriteLine("Number 2: " +n2);
}
}
class over
{
static void Main(string[] args)
{
Calculate c = new Calculate(20,-40);
c.print();
Calculate c3 = new Calculate(20, -40);
C3+=c;
c3.print();
Console.ReadKey();
}
}
}
Output:
10. 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:
11. Implement Interfaces
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InterfaceApplication
{
public interface ITransaction
{
void showTransaction();
double getAmount();
}
public class Transaction : ITransaction
{
private string tcode;
private string date;
private double amount;
public Transaction(string c, string d, double a)
{
tcode = c;
date = d;
amount = a;
}
public double getAmount()
{
return amount;
}
public void showTransaction()
{
Console.WriteLine("Transaction: {0}",tcode);
Console.WriteLine("Date: {0}", date);
Console.WriteLine("Amount: {0}", getAmount());
}
}
class Tester
{
static void Main(string[] args)
{
Transaction t1 = new Transaction("001","8/2/2019",78900.00);
Transaction t2 = new Transaction ("002", "9/2/2019" ,
451900.00);
t1.showTransaction();
t2.showTransaction();
Console.ReadKey();
}
}
}
Output:
12(a). Implement overriding methods

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

namespace ConsoleApplication4
{
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:
12(b). Implement hiding methods.

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

namespace ConsoleApplication5
{
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:
13.Copy Contents File

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

namespace file
{
class Program
{
static void Main(string[] args)
{
File.Copy("E:\\sam.txt", "E:\\sam3.txt");
File.Copy("E:\\sam.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:

You might also like