Anda di halaman 1dari 38

PROGRAMMING IN C# DISPLAYING SHAPES USING BRANCHING AND LOOPING Aim: To create a program to display different shapes using branching

and looping in C# Algorithm: Step 1: Start the process Step 2: Declare the variables Step 3: Build the coding to display different shapes by branching and looping statements. Step 4: Using Switch statement to select different shapes. Step 5: Displaying the shapes. Step 6: Stop the process. CODING: // Displaying Shapes using Branching and Looping using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication9 { class shapes { static void Main(string[] args) { int a, b, c, n; int q = 1,w=1,e=1,r=1; String str; Console.WriteLine("\n\n\t\tDISPLAYING SHAPES "); Console.WriteLine("\t\t~~~~~~~~~~~~~~~~~~~~~"); do { Console.WriteLine("\n\t1.DIAMOND"); Console.WriteLine("\t2.TRIANGLE"); Console.WriteLine("\t3.SQUARE"); Console.WriteLine("\t4.HEXAGON"); Console.WriteLine("\t5.EXIT"); Console.Write("\n\tEnter your choice: "); str = Console.ReadLine(); n = Int32.Parse(str); switch (n) { case 1: if (q == 1) { for (a = 0; a <= 6; a++) { for (b = a; b <= 6; b++) Console.Write(" "); for (c = 0; c <= a; c++) {
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 1

PROGRAMMING IN C# Console.Write(" *"); } Console.Write("\n"); } for (a = 0; a <= 6; a++) { for (b = 0; b <= a; b++) Console.Write(" "); for (c = a; c <= 6; c++) { Console.Write(" *"); } Console.Write("\n"); } q++; } else { Console.WriteLine("\t\t\t\tEnter another option:\n"); } break; case 2: if (w == 1) { for (a = 0; a <= 6; a++) { for (b = 0; b <= a; b++) { Console.Write(" *"); } Console.WriteLine(""); } w++; } else { Console.WriteLine("\t\t\t\t Enter another option:\n"); } break; case 3: if (e == 1) { for (a = 1; a <= 5; a++) { for (b = 1; b <= 5; b++) { Console.Write(" * "); } Console.WriteLine ("\n"); e++;
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 2

PROGRAMMING IN C# } } else { Console.WriteLine("\t\t\t\tEnter another option"); } break; case 4: if (r == 1) { for (a = 1; a <= 4; a++) { for (b = a; b <= 4; b++) Console.Write(" "); for (c = 0; c <= a + 3; c++) { Console.Write(" *"); } Console.WriteLine(""); } for (a = 1; a <= 3; a++) { for (b = 0; b <= a; b++) Console.Write(" "); for (c = a; c <= 7; c++) { Console.Write(" *"); } Console.WriteLine(""); } r++; } else { Console.WriteLine("\t\t\t\tEnter another choice"); } break; } } while (n < 5); } } } OUTPUT: DISPLAYING SHAPES 1. DIAMOND 2. TRIANGLE 3. SQUARE 4. HEXAGON 5. EXIT Enter your choice: 1
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 3

PROGRAMMING IN C# * *** ***** *** * Enter your choice: 2 * ** *** **** ***** Enter your choice: 3 **** **** **** **** Enter your choice: 4 *** ***** ******* ***** *** Result: Thus the above program has been executed successfully. STUDENTS MARK LIST USING METHODS, ARRAYS AND STRINGS Aim: To create a program to generating Students Mark List using Methods, Arrays and Strings in C#. Algorithm: Step 1: Start the process. Step 2: Create a class and declare the variables and methods for it.. Step 3: Create the main class and declare the object for it. Step 4: Using getdata () function, get the student details. Step 5: Using display () function, display the student mark list. Step 6: Display the student mark list. Step 7: Save and Stop the process. CODING: // Generating Students Mark List using Methods, Arrays and Strings using System; using System.Collections.Generic; using System.Text;

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

PROGRAMMING IN C# public class stud { string[] name = new string[5]; string[] clas = new string[5]; string[] regno = new string[5]; char[] grade = new char[5]; int[,] mark = new int[5, 5]; int count; float[] average = new float[5]; int[] total = new int[5]; public void getdata() { Console.WriteLine("\n\n"); Console.WriteLine("\t\t\tSTUDNET MARKSHEET"); Console.Write("\t\t\t~~~~~~~~~~~~~~~~~"); Console.WriteLine("\n\n "); Console.Write("Enter the number of student: "); count = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(" "); Console.WriteLine(" "); for (int a = 1; a <= count; a++) { Console.WriteLine("\tStudent " + a + " details"); Console.WriteLine("\t-----------------"); Console.WriteLine(""); Console.WriteLine(""); Console.Write("Enter student name: "); name[a] = Console.ReadLine(); Console.WriteLine(""); Console.Write("Enter student Register no: "); regno[a] = Console.ReadLine(); Console.WriteLine(""); Console.Write("Enter student class: "); clas[a] = Console.ReadLine(); Console.WriteLine(""); Console.WriteLine("Marks of student "); Console.WriteLine("---------------- "); Console.WriteLine(""); for (int i = 1; i <= 3; i++) { Console.Write("\tEnter mark" + i + " : "); mark[a, i] = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(""); if (mark[a, i] < 50) grade[a] = 'F'; } Console.WriteLine(" ");
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 5

PROGRAMMING IN C# Console.WriteLine(" "); } for (int i = 1; i <= count; i++) { int tot = 0; for (int j = 1; j <= 3; j++) { tot = tot + mark[i, j]; } total[i] = tot; } for (int i = 1; i <= count; i++) { average[i] = total[i] / 3; } for (int i = 1; i <= count; i++) { if (grade[i] != 'F') { if (average[i] >= 90) grade[i] = 'A'; else if (average[i] >= 80) grade[i] = 'B'; else if (average[i] >= 70) grade[i] = 'C'; else if (average[i] >= 60) grade[i] = 'D'; else if (average[i] >= 50) grade[i] = 'E'; else grade[i] = 'F'; } } } public void display() { Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("\t\t\tSTUDENTS MARKLIST"); Console.WriteLine("\t\t\t~~~~~~~~~~~~~~~~~"); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine("---------------------------------"); Console.WriteLine("Name\tReg.class\tmark1\tmark2\tmark3\tTotal\tAverage\t Grade"); Console.WriteLine("----------------------------------------------------------------------");
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 6

PROGRAMMING IN C#

for (int a = 1; a <= count; a++) { Console.WriteLine(name[a] + "\t" + clas[a] + "\t " + regno[a] + "\t" + mark[a, 1] + \t" + mark[a, 2] + "\t" + mark[a, 3] + "\t"+ total[a] + "\t" + average[a] + "\t " + grade[a]); Console.WriteLine(""); } Console.WriteLine("----------------------------------------------------------------------"); } } namespace sk { class mark { public static void Main() { stud st = new stud(); st.getdata(); st.display(); } } } OUTPUT: STUDENT MARKSHEET Enter the number of student: 5 Student 1 details --------------------Enter student name: Dinesh Enter student Register no: 12 Enter student class: MCA Marks of student -------------------Enter mark1: 65 Enter mark2: 76 Enter mark3: 87 Student 2 details -------------------Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 7

PROGRAMMING IN C#

Enter student name: Karthik Enter student Register no: 13 Enter student class: MCA Marks of student -------------------Enter mark1: 98 Enter mark2: 87 Enter mark3: 67 STUDENTS MARKLIST ~~~~~~~~~~~~~~~~~~~ ---------------------------------------------------------------------------------------------------------Name Class Reg.no Mark1 Mark2 Mark3 Total Average Grade ---------------------------------------------------------------------------------------------------------Dinesh MCA 12 65 76 87 228 76 C Karthik MCA 13 98 87 67 252 84 B Mahesh MCA 14 80 80 90 250 83 B Prathesh MCA 15 79 85 98 262 87 B Sathish MCA 16 70 60 75 205 68 C

Result: Thus the above program has been executed successfully. HOSPITAL MANAGEMENT USING STRUCTURES Aim: To write a program to display the hospital details using structures in C#. Algorithm: Step 1: Start the process. Step 2: Create a class and declare the variables and methods for it. Step 3: Create a first structure and declare two methods to get and display the Doctors details. Step 4: Create second structure and declare two methods for get and display the Patients details. Step 5: Create third structure as medicine and declare two methods for get and display the medicine details.

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

PROGRAMMING IN C# Step 6: Calculate the amount and display it. Step 7: Stop the process. CODING: // Hospital Management using Structures using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication18 { public struct doctor { public string dname; public string spec; public double con_amt; string a; public void getdata() { Console.Write("\nEnter the doctor Name :"); dname = Console.ReadLine(); Console.Write("\nThe doctor Specialist in:"); spec = Console.ReadLine(); Console.Write("\nConsultant amount :"); a = Console.ReadLine(); con_amt = double.Parse(a); } public void display() { Console.WriteLine("\n\t\tDoctor Name :" + dname); Console.WriteLine("\n\t\tThe doctor Specialist in :" + spec); Console.WriteLine("\n\t\tcosultant amount :" + con_amt); } } public struct patient { public string pname; public string disease; public string medi; public void getdata1() { Console.Write("\nEnter the patient Name :"); pname = Console.ReadLine(); Console.Write("\nPatient Address :");
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 9

PROGRAMMING IN C# medi = Console.ReadLine(); Console.Write("\nName of the disease :"); disease = Console.ReadLine(); } public void display1( ) { Console.WriteLine("\n\t\tPatient Name :" + pname); Console.WriteLine("\n\t\tPatient Address :" + medi); Console.WriteLine("\n\t\tName of the disease :" + disease); } } public struct medicine { public string med_name; public double tno, rate; string b, c; public void getdata2( ) { Console.Write("\nName of the medicine :"); med_name = Console.ReadLine(); Console.Write("\nRate of the medicine :"); b = Console.ReadLine( ); rate = double.Parse(b); Console.Write("\nTotal number of medicine:"); c = Console.ReadLine( ); tno = double.Parse(c); } public void display2() { Console.WriteLine("\n\t\tMedicine Name :" + med_name); Console.WriteLine("\n\t\tRate of the medicine :" + rate); Console.WriteLine("\n\t\tTotal number of medicine :" + tno); } } class Hospital { static void Main(string[] args) { Console.WriteLine("\n\t\ Hospital Management using Structures "); Console.WriteLine("\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ); doctor d1 = new doctor(); d1.getdata(); patient p1 = new patient(); p1.getdata1(); medicine m1 = new medicine(); m1.getdata2(); Console.WriteLine("\n\n\tOUTPUT");
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 10

PROGRAMMING IN C# Console.WriteLine("\t~~~~~~~~~~); Console.WriteLine("\n\t\t----------------------------------------------------"); d1.display(); p1.display1(); m1.display2(); Console.WriteLine("\n\t\tTotal medicine Amount :" + (m1.rate * m1.tno)); Console.WriteLine("\n\t\t-------------------------------------------------------); Console.WriteLine("\n\t\tTotal Amount :" + ((m1.rate * m1.tno) + d1.con_amt)); Console.WriteLine("\n\t\t----------------------------------------------------"); Console.ReadLine(); } } } OUTPUT: Hospital Management using Structures Enter the doctor Name : Dr.R.Raja The doctor Specialist in : General Consultant amount : 100 Enter the patient Name : K.Kumar

Patient Address : Gobi Name of the disease : Fever Name of the medicine : Dolo Rate of the medicine : 5 Total no of medicine : 10 OUTPUT ~~~~~~~ Doctor Name : Dr.R.Raja The doctor Specialist in : General Consultant amount : 100 Patient Name : k.Kumar Patient Address : Gobi Name of the disease : Fever
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 11

PROGRAMMING IN C#

Medicine Name : Dolo Rate of the medicine : 5 Total no of medicine : 10 Total medicine Amount : 50 ---------------------------------------------------Total Amount :150 ---------------------------------------------------Result: Thus the above program has been executed successfully. DISPLAYING MONTHS IN ORDER USING ENUMERATION Aim: To create a program to displaying months in order using enumeration in C#. Algorithm: Step 1: Start the process. Step 2: Declare enumeration method with enum keyword. Step 3: In that method, declares January =1. Step 4: Display the integer value and its corresponding month. Step 5: Save and run the process. Step 6: Stop the process. CODING: // Displaying Month in Order Using Enumeration using System; using System.Collections.Generic; using System.Text; namespace ENUMS { class Program { public enum month { January = 1, February, March, April, May, June, July,
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 12

PROGRAMMING IN C# August, september, october, November, December } static void Main(string[] args) { Console.WriteLine("{0} : {1}", (int)month.January, month.January); Console.WriteLine("{0} : {1}", (int)month.Febraury, month.Febraury); Console.WriteLine("{0} : {1}", (int)month.March, month.March); Console.WriteLine("{0} : {1}", (int)month.April, month.April); Console.WriteLine("{0} : {1}", (int)month.May, month.May); Console.WriteLine("{0} : {1}", (int)month.June, month.June); Console.WriteLine("{0} : {1}", (int)month.July, month.July); Console.WriteLine("{0} : {1}", (int)month.August, month.August); Console.WriteLine("{0} : {1}", (int)month.september, month.september); Console.WriteLine("{0} : {1}", (int)month.october, month.october); Console.WriteLine("{0} : {1}", (int)month.November, month.November); Console.WriteLine("{0} : {1}", (int)month.December, month.December); Console.ReadLine(); } } } OUTPUT: 1: January 2: February 3: March 4: April 5: May 6: June 7: July 8: August 9: September 10: October 11: November 12: December Result: Thus the above program has been executed successfully.

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

13

PROGRAMMING IN C# CALCULATING AREA AND VOLUMES USING INHERITANCE Aim: To create a program to calculate area and volumes using inheritance in C#. Algorithm: Step 1: Start the process. Step 2: Create a base and declare the variables and methods. Step 3: Create a derived class as volume from area. Step 4: Passing the values through arguments to volume () constructor. Step 5: Calculate the area and volumes and display the result. Step 6: Stop the process. CODING: // Calculating Area and Volumes using Inheritance using System; using System.Collections.Generic; using System.Text; public class area //base class { public double radius; public area(double r) { radius = r; } public double cir() { return (3.14*radius * radius); } } public class volume : area //derived class { public double height; public volume(double r, double h): base(r) { height = h; } public double cyl() { return (3.14 * radius * radius * height); } public double cone() { return ((.333)*3.14* radius * radius * height); }

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

14

PROGRAMMING IN C# public double cub() { return (radius * radius * radius); } }

public class inher { public static void Main() { Console .WriteLine ("\n\t\t\t INHERITANCE"); Console.WriteLine("\n\t\t\t**************\n"); volume lb = new volume(2, 30); double s = lb.cir(); Console.WriteLine("Area of Circle: \n\t " + s); double c = lb.cub(); Console.WriteLine("\n volume of Cube: \n\t"+c); double d=lb.cyl(); Console.WriteLine("\n Volume of Cylinder: \n\t" + d); double e = lb.cone(); Console.WriteLine("\n Volume of Cone:\n\t" + e); Console.WriteLine("\n"); Console.ReadLine(); } } OUTPUT: INHERITANCE *************** Area of Circle: 12.56 Volume of Cube: 8 Volume of Cylinder: 376.8 Volume of Cone: 125.4744

Result: Thus the above program has been executed successfully.

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

15

PROGRAMMING IN C# CALCULATING SIMPLE AND COMPOUND INTEREST USING POLYMORPHISM Aim: To create a program for calculate simple and compound interest using polymorphism in C#. Algorithm: Step 1: Start the process Step 2: Declare the base and derived classes. Step 3: Create an object for derived class. Step 4: Calculate the Simple and Compound interest using same name as cal (). Step 5: Displaying the Simple and Compound interest by same name display (). Step 6: Stop the process. CODING: // Calculating Simple and Compound Interest Using Polymorphism using System; using System.Collections.Generic; using System.Text; namespace poly { class account { public void display(double x,double y,double z) { Console.WriteLine("\n\t\tAccount Details"); Console.WriteLine("\n\t\t~~~~~~~~~~~~"); Console.WriteLine("\nInitial Amoount:\n "+x); Console.WriteLine("No of Years:\n " + y); Console.WriteLine("Rate of Interest: \n " + z); } } class simple : account { double sim; public double d; public virtual void cal(double x,double y,double z) { sim=(x*y*z)/100; d = x; } public virtual void display() { Console.WriteLine("\n\t\tSIMPLE INTEREST");
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 16

PROGRAMMING IN C# Console.WriteLine("\n\t\t~~~~~~~~~~~~~~~~"); Console.WriteLine("\nSimple Interest:\n"+sim); Console.WriteLine("\nTotal Amount:\n" + (sim + d)); } } class comp : simple { public double w ; double com=1,co; public override void cal(double x, double y, double z) { w = x; for (int i = 1; i <= y; i++) { com *= (1 + (z / 100)); } co = w * com; } public override void display() { Console.WriteLine("\n\t\tCOMPOUND INTEREST"); Console.WriteLine("\n\t\t~~~~~~~~~~~~~~~~~~~~"); Console.WriteLine("\nCompount Interest:\n"+(co-w)); Console.WriteLine("\nTotal Amount:\n" + co); } } class bank { public static void Main() { Console.WriteLine("\n\t\t\t Calculating Simple and Compound Interest Using Polymorphism\n"); Console.WriteLine("\t\t\t~~~~~~~~~~~~~~~~"); double b, c, d; string b1, c1, d1; Console.WriteLine(" Enter the Initial Amount"); b1 = Console.ReadLine(); b = double.Parse(b1); Console.WriteLine("\n Enter the years\n"); c1 = Console.ReadLine(); c = double.Parse(c1); Console.WriteLine("\n Enter the Rate of Interest\n"); d1 = Console.ReadLine(); d = double.Parse(d1); Console.WriteLine("\n\t\t\t\tOUTPUT\n");
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 17

PROGRAMMING IN C# account a = new account(); a.display(b,c,d); simple s = new simple(); s.cal(b, c, d); s.display(); s = new comp(); s.cal(b, c, d); s.display(); Console.ReadLine(); } } } OUTPUT: Calculating Simple and Compound Interest Using Polymorphism ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Enter the Initial Amount: 4000 Enter the years : 2 Enter the Rate of Interest: 3.45 OUTPUT ******** Account Details ~~~~~~~~~~~~ Initial Amount: 4000 No of Years: 2 Rate of Interest: 3.45

SIMPLE INTEREST ~~~~~~~~~~~~~~~ Simple Interest: 276 Total Amount: 4276 COMPOUND INTEREST ~~~~~~~~~~~~~~~~~~~ Compound Interest: 280.761 Total Amount: 4280.761 Result: Thus the above program has been executed successfully.
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 18

PROGRAMMING IN C# LIBRARY MANAGEMENT USING INTERFACE Aim: To create a program for library management using interface in C#.

Algorithm: Step 1: Start the process. Step 2: Create two interfaces and declare the methods. Step 3: Create a class based on two interfaces and declare the methods of interface. Step 4: Create another class which is based on the first class. Step 5: Declare an objects for first class and two interfaces. Step 6: Access the methods by objects of interfaces. Step7: Display the results and save the process. Step8: Stop the process CODING: // Library Management Using Interface using System; using System.Collections.Generic; using System.Text; interface book { void getbook(); } interface id { void getstd(); } class ss : book, id { public string a1,t1,b1,b2; public int s,a; public string s1, s2; public void getbook() { Console.WriteLine("Book name: "); b1 = Console.ReadLine(); Console.WriteLine("Auther name: "); b2 = Console.ReadLine(); Console.WriteLine("Book id:"); a1= Console.ReadLine(); a = Int16.Parse(a1); } public void getstd()
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 19

PROGRAMMING IN C# { Console.WriteLine("Student name:"); s1 = Console.ReadLine(); Console.WriteLine("\nClass & year:"); s2 = Console.ReadLine(); Console.WriteLine("Student ID:"); t1 = Console.ReadLine(); s = Int16.Parse(t1); } } class display:ss { public void Display(ss stu, ss get) { Console.WriteLine("\n Name of the student:\t"+ stu.s1); Console.WriteLine("\n Class & Year:\t"+stu.s2); Console.WriteLine("\n Student ID:\t\t" + stu.s); Console.WriteLine("\n Book Name:\t\t"+get.b1); Console.WriteLine("\n Auther Name:\t\t" + get.b2); Console.WriteLine("\n Book ID:\t\t" + get.a); } } class inter { public static void Main() { Console.WriteLine("\n\t\t\t Library Management Using Interface"); Console.WriteLine("\n\t\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); ss ob = new ss(); book get = (book)ob; id stu = (id)ob; get.getbook(); stu.getstd(); Console.WriteLine(\n Output); Console.WriteLine( ~~~~~~); display dis = new display(); dis.Display(ob,ob); string x = Console.ReadLine(); } }

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

20

PROGRAMMING IN C# OUTPUT: Library Management Using Interface ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Book Name: C# Author Name: Michel Book Id: 3152

Student Name: Ram kumar Class & Year: MCA & III Student Id: 07mca111 Output: ~~~~~ Name of the student: Ram kumar Class & Year:MCA & III Student ID: 07mca111 Book Name:C# Auther Name:Micheal Book ID:3152

Result: Thus the above program has been executed successfully.

MANIPULATING COMPLEX NUMBER USING OPERATOR OVERLOADING Aim: To create a program for manipulating complex number using operator overloading in C#. Algorithm: Step 1: Start the process. Step 2: Create a structure. Step 3: Overload the (+) operator to add the complex objects Step 4: Overload the (-) operator to subtract the complex objects Step 5: Passing the values to Complex ().
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 21

PROGRAMMING IN C# Step 6: Find the sum and subtraction and display the values. Step 7: Stop the process. CODING: // Manipulating Complex Number Using Operator Overloading using System; public struct Complex { public int real; public int imagin; public Complex(int real, int imagin) { this.real = real; this.imaginary = imagin; } /* Declare which operator to overload (+), the types that can be added (two Complex objects), and the return type (Complex):*/ public static Complex operator +(Complex c1, Complex c2) { return new Complex(c1.real + c2.real, c1.imagin + c2.imagin); } public static Complex operator -(Complex c1, Complex c2) { return new Complex(c1.real - c2.real, c1.imagin - c2.imagin); } // Override the ToString method to display an complex number in the suitable format: public override string ToString() { return (String.Format("{0} + {1}j", real, imagin)); } public static void Main() { Console.WriteLine("\n\t\t\tOPERATOR OVERLOADING"); Console.WriteLine("\n\t\t\t**************************"); string a1, b1, c1, d1; int a, b, c, d; Console.Write("\n\tEnter the first real Number:"); a1 = Console.ReadLine(); a = Int16.Parse(a1); Console.Write("\n\tEnter the first imanginary values:"); c1 = Console.ReadLine(); c = Int16.Parse(c1);

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

22

PROGRAMMING IN C# Console.Write("\n\tEnter the second real Number:"); b1 = Console.ReadLine(); b = Int16.Parse(b1); Console.Write("\n\tEnter the second imanginary values:"); d1 = Console.ReadLine(); d = Int16.Parse(d1); Complex num1 = new Complex(a, c); Complex num2 = new Complex(b, d); // Add two Complex objects (num1 and num2) through the overloaded plus operator: Complex sum = num1 + num2; Complex sub = num1 - num2; // Print the numbers and the sum using the overriden ToString method: Console.Write("\n\nOUTPUT"); Console.WriteLine("\n~~~~~~"); Console.Write("\n\n\tFirst complex number : {0}", num1); Console.Write("\n\n\tSecond complex number : {0}", num2); Console.Write("\n\n\tThe sum of the two numbers : {0}", sum); Console.Write("\n\n\tThe subtraction of the two numbers:{0}", sub); Console.ReadLine(); } } OUTPUT: OPERATOR OVERLOADING ************************* Enter the first real Number: 34 Enter the first imaginary values: 23 Enter the second real Number: 12 Enter the second imaginary values: 11 OUTPUT ~~~~~~~ First complex number: 34 + 23j Second complex number: 12 + 11j

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

23

PROGRAMMING IN C# The sum of the two numbers: 46 + 34j The subtraction of the two numbers: 22 + 12j Result: Thus the above program has been executed successfully.

DELEGATE AND EVENTS Aim: To create a program for delegate and events using C#. Algorithm: Step 1: Start the process Step 2: Declare the delegate function. Step 3: Define a class and declare the event. Step 4: Define the method as RaiseEvent with two arguments. Step 5: Define two methods as Add (), Subtract () for add and subtract the two values. Step 6: Display the result and save the process. Step7: Stop the process. CODING: // Program for Delegate and Events using System; using System.Collections.Generic; using System.Text; namespace delegate { class Program { public delegate void MyDelegate(int a, int b); public class XX { public event MyDelegate MyEvent; public void RaiseEvent(int a, int b) { MyEvent(a, b); Console.WriteLine("Event Raised"); } public void Add(int x, int y) {
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 24

PROGRAMMING IN C# Console.WriteLine("Add Method {0}", x + y); } public void Subtract(int x, int y) { Console.WriteLine("Subtract Method {0}", x - y); } }

static void Main(string[] args) { XX obj = new XX(); obj.MyEvent += new MyDelegate(obj.Add); obj.MyEvent += new MyDelegate(obj.Subtract); obj.RaiseEvent(20, 10); Console.ReadLine(); } } } OUTPUT: Add Method 30 Subtract Method 10 Event Raised Result: Thus the above program has been executed successfully.

ERROR AND EXCEPTIONS Aim: To create a program for error and exception using C#. Algorithm: Step 1: Start the process Step 2: Declare the classes and methods Step 3: In try, get the two values and divide it. Step 4: If the denominator is zero, catch method displays the error message as Divide by Zero. Step 5: If the numerator is zero, catch will displays as arithmetic exception. Step 6: Otherwise display the result Step 7: Stop the process.

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

25

PROGRAMMING IN C# CODING: // Program for Error and exceptions using System; using System.Collections.Generic; using System.Text; namespace error_excep { public class test { static void Main(string[] args) { test t = new test(); t.tfun(); Console.ReadLine(); } public void tfun() { try { double a; double b; string w, q; Console.WriteLine("enter a"); w = Console.ReadLine(); a = double.Parse(w); Console.WriteLine(" enter b"); q = Console.ReadLine(); b = double.Parse(q); Console.WriteLine("{0}/{1}={2}", a, b, divde(a, b)); } catch (System.DivideByZeroException) { Console.WriteLine("Divide by zero"); } catch (System.ArithmeticException) { Console.WriteLine("arithmetic exception"); Console.WriteLine("0"); } catch { Console.WriteLine("unknown exception"); } } public double divde(double a, double b)
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 26

PROGRAMMING IN C# { if (b == 0) throw new System.DivideByZeroException(); if (a == 0) throw new System.ArithmeticException(); return a / b; } } } OUTPUT Enter a: 5 Enter b: 0 Divide by zero

Result: Thus the above program has been executed successfully.

CALCULATOR WIDGET USING WINDOWS Aim: To create a calculator widget using windows application in C# environment. Algorithm: Step 1: Start the process. Step 2: Select the windows application. Step 3: Using toolbox, put the buttons and textboxes into the form Step 4: Declare the variables. Step 5: Write the coding to get values on the textbox and perform the operations as add, subtract, multiplication and subtraction. Step 6: Calculate the results Step 7: Display the results in the textbox. Step 8: Stop the process. CODING: // To Build a Calculator widget using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing;
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 27

PROGRAMMING IN C# using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static string m; public static float a, b,c; public int q, w, e, r; private void button1_Click(object sender, EventArgs e) { m = textBox1.Text + "1"; textBox1 .Text=m; } private void button2_Click(object sender, EventArgs e) { m = textBox1.Text + "2"; textBox1.Text = m; } private void button3_Click(object sender, EventArgs e) { m = textBox1.Text + "3"; textBox1.Text = m; } private void button5_Click(object sender, EventArgs e) { m = textBox1.Text + "4"; textBox1.Text = m; } private void button6_Click(object sender, EventArgs e) { m = textBox1.Text + "5"; textBox1.Text = m; } private void button7_Click(object sender, EventArgs e) { m = textBox1.Text + "6"; textBox1.Text = m; } private void button9_Click(object sender, EventArgs e)
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 28

PROGRAMMING IN C# { m = textBox1.Text + "7"; textBox1.Text = m; } private void button10_Click(object sender, EventArgs e) { m = textBox1.Text + "8"; textBox1.Text = m; } private void button11_Click(object sender, EventArgs e) { m = textBox1.Text + "9"; textBox1.Text = m; } private void button13_Click(object sender, EventArgs e) { m = textBox1.Text + "0"; textBox1.Text = m; } private void button14_Click(object sender, EventArgs e) { textBox1.Text = ""; q = 0; m = ""; a = 0; b=0; } private void button16_Click(object sender, EventArgs e) { a = float.Parse(textBox1.Text); textBox1.Text = ""; q = 1; } private void button15_Click(object sender, EventArgs e) { if (q == 1) { b = float.Parse(textBox1.Text); textBox1.Text = (a + b).ToString(); } if (q == 2) { b = float.Parse(textBox1.Text); textBox1.Text = (a - b).ToString(); } if (q == 3) { b = float.Parse(textBox1.Text); textBox1.Text = (a * b).ToString();
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 29

PROGRAMMING IN C# } if (q == 4) { b = float.Parse(textBox1.Text); textBox1.Text = (a / b).ToString(); } } private void button12_Click(object sender, EventArgs e) { a = float.Parse(textBox1.Text); textBox1.Text = ""; q = 2; } private void button8_Click(object sender, EventArgs e) { a = float.Parse(textBox1.Text); textBox1.Text = ""; q = 3; } private void button4_Click(object sender, EventArgs e) { a = float.Parse(textBox1.Text); textBox1.Text = ""; q = 4; } } } OUTPUT:

RESULT: Thus the above program has been executed successfully.


Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 30

PROGRAMMING IN C# MULTI MODULE ASSEMBLY Aim: Create the multi-module assemblies that combine into single assemblies for using C# .net. Algorithm: Step1: Start the program and select the . Step2: Make file begins by defining the assembly Step3: Defines the directories you'll use, putting the output in a bin directory beneath the current directory and retrieving the source code from the current directory: BIN=.\bin SRC=. DEST=.\bin Step4: Places the assembly (MySharedAssembly.dll) in the destination directory (bin). It tells nmake (the program that executes the makefile) that the assembly consists of the metadata and the modules, and it provides the command line required to build the assembly. Step5: The compile line builds the library and adds the modules, putting the output into the assembly file MySharedAssembly.dll: Sttep6: Run the program and display the results Step7: Stop the process.

The Fraction class namespace ProgCS { using System; public class Fraction { private int numerator; private int denominator; public Fraction(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } public Fraction Add(Fraction rhs) { if (rhs.denominator != this.denominator) { throw new ArgumentException( "Denominators must match"); } return new Fraction(
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 31

PROGRAMMING IN C# this.numerator + rhs.numerator, this.denominator); } public override string ToString( ) { return numerator + "/" + denominator; } } } The Calculator namespace ProgCS { using System;

public class myCalc { public int Add(int val1, int val2) { return val1 + val2; } public int Mult(int val1, int val2) { return val1 * val2; } } } The complete makefile for a multi-module assembly ASSEMBLY= MySharedAssembly.dll BIN=.\bin SRC=. DEST=.\bin CSC=csc /nologo /debug+ /d:DEBUG /d:TRACE MODULETARGET=/t:module LIBTARGET=/t:library EXETARGET=/t:exe REFERENCES=System.dll MODULES=$(DEST)\Fraction.dll $(DEST)\Calc.dll METADATA=$(SRC)\AssemblyInfo.cs all: $(DEST)\MySharedAssembly.dll # Assembly metadata placed in same module as manifest $(DEST)\$(ASSEMBLY): $(METADATA) $(MODULES) $(DEST)
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 32

PROGRAMMING IN C# $(CSC) $(LIBTARGET) /addmodule:$(MODULES: =;) /out:$@ %s # Add Calc.dll module to this dependency list $(DEST)\Calc.dll: Calc.cs $(DEST) $(CSC) $(MODULETARGET) /r:$(REFERENCES: =;) /out:$@ %s # Add Fraction $(DEST)\Fraction.dll: Fraction.cs $(DEST) $(CSC) $(MODULETARGET) /r:$(REFERENCES: =;) /out:$@ %s $(DEST):: !if !EXISTS($(DEST)) mkdir $(DEST) !endif The makefile begins by defining the assembly you want to build: ASSEMBLY= MySharedAssembly.dll It then defines the directories you'll use, putting the output in a bin directory beneath the current directory and retrieving the source code from the current directory: BIN=.\bin SRC=. DEST=.\bin Build the assembly as follows: $(DEST)\$(ASSEMBLY): $(METADATA) $(MODULES) $(DEST) $(CSC) $(LIBTARGET) /addmodule:$(MODULES: =;) /out:$@ %s This places the assembly (MySharedAssembly.dll) in the destination directory (bin). It tells nmake (the program that executes the makefile) that the assembly consists of the metadata and the modules, and it provides the command line required to build the assembly. The metadata is defined earlier as: METADATA=$(SRC)\AssemblyInfo.cs The modules are defined as the two DLLs: MODULES=$(DEST)\Fraction.dll $(DEST)\Calc.dll The compile line builds the library and adds the modules, putting the output into the assembly file MySharedAssembly.dll: $(DEST)\$(ASSEMBLY): $(METADATA) $(MODULES) $(DEST) $(CSC) $(LIBTARGET) /addmodule:$(MODULES: =;) /out:$@ %s To accomplish this, nmake needs to know how to make the modules. Start by telling nmake how to create Calc.dll. You need the Calc.cs source file for this; tell nmake on the command line to build that DLL: $(DEST)\Calc.dll: Calc.cs $(DEST) $(CSC) $(MODULETARGET) /r:$(REFERENCES: =;) /out:$@ %s Then do the same thing for Fraction.dll: $(DEST)\Fraction.dll: Fraction.cs $(DEST) $(CSC) $(MODULETARGET) /r:$(REFERENCES: =;) /out:$@ %s

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

33

PROGRAMMING IN C# MySharedAssembly.dll

The manifest for MySharedAssembly.dll

Result: Thus the above program has been executed successfully.

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

34

PROGRAMMING IN C# Windows Application Aim: Create a windows application in c# to calculate the average of students marks and weights. Algorithm: Step1: Start the program and select the windows application. Step2: Design the first form using toolbox Step3: Add the new forms by project->Add new Item->windows forms Step4: Link the new forms with first form Step5: Write the coding to calculate average marks and weights in form2 and form3 Sttep6: Run the program and display the results Step7: Stop the process. CODING: Windows Application //Main Prog.cs (Form1.cs) using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { new Form2().Show(); } private void button2_Click(object sender, EventArgs e) { new Form3().Show(); } } }
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 35

PROGRAMMING IN C#

//Form2.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void cmdeq_Click(object sender, EventArgs e) { int a, b, c,avg; a = int.Parse(textBox1.Text); b = int.Parse(textBox2.Text); c = int.Parse(textBox3.Text); avg = (a + b + c) / 3; textBox4.Text = avg.ToString(); } } } //Form3.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form3 : Form { public Form3() {
Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT 36

PROGRAMMING IN C# InitializeComponent(); }

private void cmdEq_Click(object sender, EventArgs e) { double a, b, c,avg; a = double.Parse(textBox1.Text); b = double.Parse(textBox2.Text); c = double.Parse(textBox3.Text); avg = (a + b + c) / 3.0; textBox4.Text = avg.ToString(); } } } OUTPUT:

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

37

PROGRAMMING IN C#

Result: Thus the above program has been executed successfully.

Ms. R. MALATHI, ASST. PROF. OF CS, HHRC, PDKT

38

Anda mungkin juga menyukai