Sunteți pe pagina 1din 58

1043006

C# PRACTICAL
PRACTICAL NO: - 01 TITLE:- Write a program to perform following task a. Declare two variable x and y at float types variables. b. Declare m as an integer variable c. Assign the value 75.86 to x and 143.48 y d. Assign the sum of x and y to m e. Display the value of m f. Comment on the output CODING:using System; class edit { public static void Main() { float x,y; int m; x=75.86F; y=143.48F; m=(int)(x+y); Console.WriteLine("The sum of two float number is:"+m); } } OUTPUT:-

1043006 PRACTICAL NO: - 02 TITLE:- Write a program to demonstrate the use of concept of Boxing and Unboxing CODING:using System; class edit { public static void Main() { int m=100; object om=m; m=20; Console.WriteLine("The value of m is : "+m); Console.WriteLine("The value of om is : "+om); int n=(int)om; Console.WriteLine("The value of n is : "+n);//n=100 } } OUTPUT:-

PRACTICAL NO: - 03 TITLE:- Write a program to read interactively two integers using the method Console.ReadLine () and int.Parse and display their sum, difference, product, division and modulus division.

1043006 CODING: using System; class edit { public static void Main() { int x,y; Console.WriteLine("Enter the first number:"); x=(int.Parse)(Console.ReadLine()); Console.WriteLine("Enter the second number:"); y=(int.Parse)(Console.ReadLine()); int sum=x+y; Console.WriteLine("The Sum of two number is : "+ sum); int diff=x-y; Console.WriteLine("The difference of two number is:" +diff); int mul=x*y; Console.WriteLine("The Multiplication of two number is: "+mul); int div=x/y; Console.WriteLine("The Division of two number is : "+ div); int mod=x%y; Console.WriteLine("The Modulus of two number is : "+ mod); } } OUTPUT:-

PRACTICAL NO: - 04 TITLE :- State why the expression x-y=10 is invalid but the expression x-(y=100) execute the program to demonstrate the answer.

1043006 CODING: A) Expression x-y=10 using System; class edit { public static void Main() { int x=0; int y; int r; r=(x-y=100); Console.WriteLine("The value is :" +r); } } OUTPUT:

B) Expression x-(y=100) CODING: using System; class edit { public static void Main() { int x=0; int y; int r; r=x-(y=100);

1043006 Console.WriteLine("The value is :" +r); } } OUTPUT:

PRACTICAL NO: - 05 TITLE:- Given the radius of 12.5cm.Write a program to compute its circumference and area and gives its value. CODING: using System; class edit { public static void Main() { float rad=12.5F; double area,circum; area=(3.14)*(rad*rad); circum=2*3.14*rad; Console.WriteLine("The area of the circle is : " + area); Console.WriteLine("The circumfurance of the circle is:"+circum); } }

1043006

OUTPUT :

PRACTICAL NO: - 06 TITLE = Write a program to add all the odd numbers from 0 to 20 use a simple if and goto statements to form a loop of operations. CODING: using System; class usegotoloop { public static void Main() { int x=0; int count=0; int sum=0; ifgo: if(x<=20) { if(x%2!=0) { Sum=sum+x; Count=count+1; } x=x+1; goto ifgo;

1043006 } Console.WriteLine ("The sum of odd numbers is:"+sum); Console.WriteLine ("The total numbers of odd numberis:"+count); } } OUTPUT:

PRACTICAL NO: - 07 TITLE:- Admission for professional course is subject to the following condition a) Marks in mathematics >= 60 b) Marks in Physics >=50 c) Marks in chemistry >= 40 d) Marks in all subject >= 200 OR e) Total in mathematics and physics >=150. CODING: using System; class edit { public static void Main() { int m,p,c; int at,mpt; Console.WriteLine("Enter the marks of the maths:"); m=(int.Parse)(Console.ReadLine()); Console.WriteLine("Enter the marks of the physics:"); p=(int.Parse)(Console.ReadLine());

1043006 Console.WriteLine("Enter the marks of the chemistry:"); c=(int.Parse)(Console.ReadLine()); at=(m+p+c); mpt=(m+p); if((m>=60 && p>=50 && c>=40) && (at>=200 || mpt>=150)) { Console.WriteLine("You are eligible for the professional course admission "); } else { Console.WriteLine("You are not eligible for the professional course admission "); } } } OUTPUT:-

PRACTICAL NO: - 08 TITLE:- Write a Program to find total number of and sum of integer greater than 100 and less than 200 that are divisible by 7.. CODING:using System; class edit { public static void Main() { int count=1; int sum=0; for(int i=100;i<200;i++) { 8

1043006 if(i%7==0) { count=count+1; sum=sum+i; } } Console.WriteLine("The total numbers that are divisible by 7 are : " + count); Console.WriteLine("The sum of numbers that are divisible by 7 is : "+sum); } } OUTPUT:-

PRACTICAL NO: - 09 TITLE = Write a Program to that will read the value of x and evaluates the function. Y= {1if x>0} {0 if x=0} {-1 if x<0} Using nested if statement else if statement ,conditional opearator. CODING: a) Using if statement using System; class nestedif { public static void Main() { int x,y; Console.WriteLine("Enter the value of x: "); 9

1043006 x=(int.Parse)(Console.ReadLine()); if(x>0) { y=1; Console.WriteLine("The value of y is :" +y); } if(x==0) { y=0; Console.WriteLine("The value of y is :"+y); } if(x<0) { y=(-1); Console.WriteLine("The value of y is :"+y); }} } OUTPUT:-

Using else if statement: using System; class firstmat { public static void Main() { Console.WriteLine("Enter the value of the x:"); int x=int.Parse(Console.ReadLine()); if(x>0) Console.WriteLine("y = "+1); else if(x==0) Console.WriteLine("y= "+0);

10

1043006 else Console.WriteLine("y= "+(-1)); } } OUTPUT:-

Using conditional operator CODING: using System; class firstmat { public static void Main() { int y; Console.WriteLine("Enter the value of the x:"); int x=int.Parse(Console.ReadLine()); y=x>1?1:-1; if(x==0) y=0; Console.WriteLine("The value of y is : "+y); } }

OUTPUT:

11

1043006

PRACTICAL NO: - 10 TITLE :-Write a Program to compute the sum of digits of a given integer. CODING: using System; class DigitSum { public static void Main() { int x; int rem; int sum = 0; Console.WriteLine("Enter The Number: "); x = (int.Parse)(Console.ReadLine()); while (x > 0) { rem = x % 10; sum = sum + rem; x = x / 10; } Console.WriteLine("The addition of given digits of number is:" + sum); } } OUTPUT: 12

1043006

PRACTICAL NO: -11 TITLE:- Write a Program using do while loop to calculate and print the first m Fibonacci numbers. CODING: using System; class DigitSum { public static void Main() { int x,c=0; int i=0; int a=1; int b=1; Console.WriteLine("Enter The Number upto which you want to print the fibonacci number:" ); x=(int.Parse)(Console.ReadLine()); Console.WriteLine("The fibonacci series is as follow:"); Console.Write(a+ " " + b + " "); do { c=a+b; a=b; b=c;

13

1043006 Console.Write(c+ " "); i++; } while(i<(x-2)); } } OUTPUT:

PRACTICAL NO: -12 TITLE:- Write a Program to print the following outputs using for loops a) 1 c) 1 b)$ $ $ $ 22 2 2 $$$ 333 3 3 3 $$ 4444 4 4 4 4 $ CODING:A)using System; class pattern { public static void Main() { int x; Console.Write("Enter The Number of rows : " ); x=(int.Parse)(Console.ReadLine()); for(int i=1;i<=x;i++) { for(int j=1;j<=i;j++)

14

1043006 { Console.Write(i+ " "); } Console.WriteLine(); } } } OUTPUT:

B) CODING: using System; class pattern { public static void Main() { int x; Console.Write("Enter The Number of rows : " ); x=(int.Parse)(Console.ReadLine()); for(int i=1;i<=x;i++) { for(int j=x;j>i;j--) { Console.Write(" "); } for(int k=1;k<=i;k++) { 15

1043006 Console.Write(i+" "); } Console.WriteLine(); } } } OUTPUT:

C) using System; class pattern { public static void Main() { int x; Console.Write("Enter The Number of rows : " ); x=(int.Parse)(Console.ReadLine()); for(int i=1;i<=x;i++) { for(int j=x;j>=i;j--) { Console.Write(" " + "$" + " "); } Console.WriteLine(); } } } 16

1043006

OUTPUT:

PRACTICAL NO: -13 TITLE:- Demonstrate the typical use of the following jump statement. a) break b) continue c) goto CODING:using System; class GotoLabel { 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; } 17

1043006 loop1:continue; } Console.WriteLine("Termination by BREAK"); } } OUTPUT:

PRACTICAL NO: -14 TITLE:- Write a program using while loop to reverse the digit of a number. CODING:using System; class ContinueBreak { public static void Main() { int num; int rev=0,rem; Console.Write("Enter the number to reversed:"); num=(int.Parse)(Console.ReadLine()); while(num>0) { rem=num%10; rev=rev*10+rem; num=num/10;

18

1043006 } Console.WriteLine("The reverse of the number is :"+rev); } } OUTPUT:

PRACTICAL NO: -15 TITLE:- Write a program to print the floyds triangle. 1 23 456 7 8 9 10 .. 79.91 CODING:using System; class umat { public static void Main() { int num; int k=1;

19

1043006 Console.Write("Enter the number of rows:"); num=(int.Parse)(Console.ReadLine()); for(int i=1;i<=num;i++) { for(int j=1;j<=i;j++) { Console.Write(k+" "); k=k+1; } Console.WriteLine(); } } } OUTPUT:

PRACTICAL NO: -16 TITLE:- Write a program to print the following output. 1 01 101 0101 10101 Ask the number of rows from the user. CODING:

20

1043006 using System; class floyd { public static void Main() { int num; Console.Write("Enter the number of rows:"); num=(int.Parse)(Console.ReadLine()); for(int i=1;i<=num;i++) { Console.Write(" "); for(int j=1;j<=i;j++) { if(i%2==0) { if(j%2==0) { Console.Write(1+" "); } else { Console.Write(0+" "); } } else { if(j%2==0) { Console.Write(0+" "); } else { Console.Write(1+" "); } } } Console.WriteLine(); } } }

OUTPUT :

21

1043006

PRACTICAL NO: -17 TITLE:- Write a program to print the following output. 100 010 001 Ask the number of rows from the user. CODING: using System; class umat { public static void Main() { int num; Console.Write("Enter the number of rows:"); num=(int.Parse)(Console.ReadLine()); int count=(num-1); for(int i=1;i<=num;i++) { for(int j=1;j<=(i-1);j++) 22

1043006 { Console.Write(0+" "); } Console.Write(1+" "); for(int k=count;k>0;k--) { Console.Write(0+" "); } count=count-1; Console.WriteLine(); } }} OUTPUT:

PRACTICAL NO: -18 TITLE:- Write a program to calculate the standard deviation of an array of values. Use methods standard and mean to calculate the standard deviation and mean of the values. . CODING: using System; class edit { public double mean(params int[] z) { int sum=0;

23

1043006 for(int i=0;i<z.Length;i++) { sum=sum+z[i]; } double avg=sum/(double)(z.Length); return avg; } public void standard(int a,int b,double c) { double v=(double)((a/b)-(c*c)); double sd=(double)(Math.Sqrt(v)); Console.WriteLine("The standard deviation is : "+sd); } public static void Main() { edit obj=new edit(); int i; int n=0; int fx=0; int[] x=new int[5]; int[] f=new int[5]; Console.WriteLine("Enter the value of the variate x:"); for(i=0;i<5;i++) { x[i]=int.Parse(Console.ReadLine()); } Console.WriteLine("Enter the value of the frequency f:"); for(i=0;i<5;i++) { f[i]=int.Parse(Console.ReadLine()); } for(i=0;i<5;i++) { fx=fx+(f[i]*(x[i]*x[i])); n=n+f[i]; } double m=obj.mean(x);

24

1043006 Console.WriteLine("The mean is :" +m); obj.standard(fx,n,m); } } OUTPUT:

PRACTICAL NO: -19 TITLE:- Write a program that uses to sort and array of integer ascending and descending order. . CODING: using System; class umat { public static void Main() { int[] number={43,67,89,90,43,56}; int n=number.Length; Console.Write("Given list:"); for(int i=0;i<n;i++) { Console.Write(" "+number[i]); }

25

1043006 Console.Write("\n"); for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { if(number[i]<number[j]) { int temp=number[i]; number[i]=number[j]; number[j]=temp; } } } Console.Write("Sorted list:"); for(int i=0;i<n;i++) { Console.Write(" "+number[i]); } Console.WriteLine(); Console.Write("Array sorted in ascending order:"); for(int j=n-1;j>=0;j--) { Console.Write(" "+number[j]); } Console.WriteLine(); } }

OUTPUT:

26

1043006

PRACTICAL NO: -20 TITLE:- Develop the program that uses a method to find a largest and a smallest number from an array of integer. CODING: using System; class umat { public static void Main() { umat obj=new umat(); int i; int[] num=new int[5]; Console.WriteLine("Enter five element into the array:"); for(i=0;i<num.Length;i++) { num[i]=int.Parse(Console.ReadLine()); } Console.WriteLine("The elements present in the array are:"); for(i=0;i<num.Length;i++) { Console.Write(" "+num[i]); } 27

1043006 Console.WriteLine(); obj.find(num); } Public void find(params int[] num1) { int max=num1[0]; for(i=1;i<num1.Length;i++) { if(max < num1[i]) max=num1[i]; } Console.WriteLine("The maximum of the given numbers is:"+max); int min=num1[0]; for(i=1;i<num1.Length;i++) { if(min > num1[i]) min=num1[i]; } Console.WriteLine("The minimum of the given number:"+min);

} } OUTPUT:

PRACTICAL NO: -21

28

1043006 TITLE:- Develop the program to search the number from array of integer. CODING: using System; class umat { public static void Main() { int i; int flag=1; int[] num=new int[5]; Console.WriteLine("Enter five element into the array:"); for(i=0;i<num.Length;i++) { num[i]=int.Parse(Console.ReadLine()); } Console.WriteLine("Enter the number you want to search in array"); int n=int.Parse(Console.ReadLine()); for(i=0;i<num.Length;i++) { if(num[i]==n) { flag=0; break; } else { flag=1; } } if(flag==0) Console.WriteLine("The number is present in the array"); else Console.WriteLine("The number is not present in the array"); } }

OUTPUT:

29

1043006

PRACTICAL NO: -22 TITLE:- Write a program that return a true if its argument is prime and return false otherwise.

CODING: using System; class edit { public bool Prime(int x) { int flag=0; bool r; for(int i=2;i<x;i++) { if(x%i==0) { flag=1; break; } else { flag=0; }

30

1043006 } if(flag==0) r=true; else r=false; return r; } public static void Main() { int i; bool result; Console.WriteLine("Enter the number:"); i=int.Parse(Console.ReadLine()); edit obj=new edit(); result=obj.Prime(i); Console.WriteLine(result); } } OUTPUT:

PRACTICAL NO: -23

31

1043006 TITLE:- Write a void type method that takes two int type values parameters and 1 int type out parameter and return the product of two value parameter through the output parameter. CODING: using System; class edit { public void product(int x,int y, out int z) { z=(x*y); } public static void Main() { int a,b,c; Console.WriteLine("Enter the first number:"); a=int.Parse(Console.ReadLine()); Console.WriteLine("Enter the second number:"); b=int.Parse(Console.ReadLine()); edit obj=new edit(); obj.product(a,b,out c); Console.WriteLine("The result of multiplication is:"+c); } } OUTPUT:

PRACTICAL NO: -24

32

1043006 TITLE:- Write a method that takes 3 values as input parameter and Returns the smallest of three values. CODING: using System; class edit { public int Compare(params int[] z) { int min=z[0]; for(int i=1;i<z.Length;i++) { if(min > z[i]) min=z[i]; } return min; } public static void Main() { int a,b,c; Console.WriteLine("Enter the first number:"); a=int.Parse(Console.ReadLine()); Console.WriteLine("Enter the second number:"); b=int.Parse(Console.ReadLine()); Console.WriteLine("Enter the third number:"); c=int.Parse(Console.ReadLine()); edit obj=new edit(); int result=obj.Compare(a,b,c); Console.WriteLine("The Smallest number is :"+result); } }

OUTPUT:

33

1043006

PRACTICAL NO: -25 TITLE:- Write a method that takes 3 values as input parameter and Returns the largest of three values. CODING: using System; class edit { public int Compare(params int[] z) { int max=z[0]; for(int i=1;i<z.Length;i++) { if(max < z[i]) max=z[i]; } return max; } public static void Main() { int a,b,c; Console.WriteLine("Enter the first number:"); a=int.Parse(Console.ReadLine()); Console.WriteLine("Enter the second number:"); b=int.Parse(Console.ReadLine()); 34

1043006

Console.WriteLine("Enter the third number:"); c=int.Parse(Console.ReadLine()); edit obj=new edit(); int result=obj.Compare(a,b,c); Console.WriteLine("The Largest number is :"+result); } } OUTPUT:

PRACTICAL NO: -26 TITLE:- Write a method that takes an array as an input parameter uses 2 methods a) To find largest array element b) To compute the average of array element. CODING: using System; class edit { public void avgmax(params int[] z) { int max=z[0];

35

1043006 int sum=z[0]; for(int i=1;i<z.Length;i++) { if(max < z[i]) { max=z[i]; } sum=sum+z[i]; } Console.WriteLine("The maximum of the enter element is : "+max); int avg=(sum)/(z.Length); Console.WriteLine("The avarage of the entered number is :"+avg); } public static void Main() { edit obj=new edit(); int[] num=new int[10]; Console.WriteLine("Enter the 10 numbers into the array : "); for(int i=0;i<num.Length;i++) { num[i]=(int.Parse)(Console.ReadLine()); } Console.WriteLine(); Console.WriteLine("The element present in the array are: "); for(int i=0;i<num.Length;i++) { Console.Write(" " +num[i]); } Console.WriteLine(); obj.avgmax(num); } }

OUTPUT:

36

1043006

PRACTICAL NO: -27 TITLE:- Write a program to accept a shopping list of five items from the command line and stored them in string type array and then print the list in alphabetical order. CODING: using System; class edit { public static void Main(String [] arg) { int i; string[] str=new string[5]; if(arg.Length==5) { for(i=0;i<5;i++) { str[i]=arg[i]; } } else { Console.WriteLine("You can enter only five element at a time!!"); }

37

1043006 for(i=0;i<str.Length;i++) { Console.WriteLine(" "+str[i]); } Array.Sort(str); Console.WriteLine("The elements arraange in the alphabetical order are as follow:"); for(i=0;i<str.Length;i++) { Console.WriteLine(" "+str[i]); } } } OUTPUT:

PRACTICAL NO: -28 TITLE:- Create a class named Double. This class contains three double numbers. Overload +,-,*,/ operators so that they can be applied to the objects of the class Double. Write a c# program to test it. CODING:CODING:using System; class Double {

38

1043006 double i; double j; Double(double d1, double d2) { this.i = d1; this.j = d2; } public static Double operator +(Double r1,Double r2) { return new Double(r1.i+r2.i,r1.j+r2.j); } public static Double operator -(Double r1,Double r2) { return new Double(r1.i-r2.i,r1.j-r2.j); } public static Double operator *(Double r1,Double r2) { return new Double(r1.i*r2.i,r1.j*r2.j); } public static Double operator /(Double r1,Double r2) { return new Double(r1.i/r2.i,r1.j/r2.j); } public void displayOutput() { Console.WriteLine(i+ " " +j); } static void Main(string[] args) { Double s1 = new Double(5.7,16.5); Double s2 = new Double(7.9,9.2); Double s3 =s1 + s2; Console.WriteLine("The sum of the two numbers: "); s3.displayOutput(); s3=s1 - s2; Console.WriteLine("The subtraction of the two numbers: "); s3.displayOutput(); s3=s1 * s2; Console.WriteLine("The multiplication of the two numbers: "); s3.displayOutput(); s3=s1 / s2; Console.WriteLine("The division of the two numbers: "); s3.displayOutput(); } }

39

1043006 OUTPUT:-

PRACTICAL NO: -29 TITLE:- Create a class example to hold one integer value. Include a constructor to display the message Object is created and a destructor to display the message Object is destroyed. Write a c# program to demonstrate the creation and destruction of objects of example class. CODING:using System; class example { int y; example(int c) { y=c; Console.WriteLine("\nOBJECT IS BORN.\n\n"); } void x() { Console.WriteLine("value is " +y); Console.WriteLine("NOW X IS ALIVE.\n"); } ~example() { Console.WriteLine("OBJECT DIES."); } static void Main(string[] args) { example z = new example(114); z.x();

40

1043006 Console.ReadLine(); } } OUTPUT:-

PRACTICAL NO: -30 TITLE:- Write a c# program that throws NumberNotInRange exception if number entered by user is not in the range 1 to 100. CODING:using System; class NumberNotInRange : System.Exception { public static void Main(string[] args) { int no = int.Parse(args[0]); try { if(no<1 || no>100) { throw new NumberNotInRange(); } } catch(NumberNotInRange noExcep) { Console.WriteLine("Exception is caught"); Console.WriteLine("Exception message " + noExcep.Message); } } 41

1043006 } OUTPUT:-

PRACTICAL NO: -31 TITLE:- Write a program to convert the temperature in Fahrenheit(starting from 98.5 to 102 in steps of 0.1) to Celsius using the formula C=(F-32)/1.8 and display the values in a tabular form. CODING:using System; class FahrenCelcius { static void Main(string[] args) { double far,cel; Console.WriteLine("Fahrenheit Celcius"); for(far = 98.5;far <= 102.0;far = far+0.1) { cel = (far - 32) / 1.8; Console.WriteLine("{0,20}\t{1,20}",far, cel); } } }

42

1043006 OUTPUT:-

PRACTICAL NO: -32 TITLE:- Write a program that Prints the following form * * * * * * * * * * * * * * * CODING:using system; class asterikprint { static void main(string[] args) {

43

1043006 int i,sp,k,u; u=5; for(i=0;i<5;++i) { for(sp=5;sp>u;--sp) { console.write(" "); } for(k=sp;k>0;--k) { console.write(" *"); } --u; console.writeline(); } } } OUTPUT:-

PRACTICAL NO: -33 TITLE:- Given a list of marks ranging from 0 to 100,write a program to compute and print the number of students who have obtained marks. (a) In the range 81 to 100 (b) In the range 61 to 80 (c) In the range 41 to 60, and (d) In the range 0 to 40 CODING:using System; 44

1043006 class StudMarksObt { public static void Main(string []args) { int[] students = { 45, 67, 89, 57, 34, 95, 0, 76, 24 }; int first = 0, second = 0, third = 0, fail = 0; for (int i = 0; i < students.Length; i++) { if (students[i] >= 81 && students[i] <= 100) first++; if (students[i] >= 61 && students[i] <= 80) second++; if (students[i] >= 41 && students[i] <= 60) third++; if (students[i] >= 0 && students[i] <= 40) fail++; } Console.WriteLine("No. of students obt. mrks bet 81 and 100 are :" + first); Console.WriteLine("No. of students obt. mrks bet 61 and 80 are :" + second); Console.WriteLine("No. of students obt. mrks bet 41 and 60 are :" + third); Console.WriteLine("No. of students obt. mrks bet 0 and 40 are :" + fail); Console.Read(); } }

OUTPUT:-

45

1043006

PRACTICAL NO: -34 TITLE:- Write a program to show the implementation of multiple interfaces. CODING:using system; interface addition { int add(); } interface subtraction { int sub(); } interface multiplication { int multiply(); } interface division { int divide(); } class calculator : addition, subtraction, multiplication, division { int a, b; public calculator(int x, int y) { this.a = x; this.b = y; }

46

1043006 public int add() { return (a + b); } public int sub() { return (a - b); } public int multiply() { return (a * b); } public int divide() { return (a / b); } } class prac40 { public static void main() { calculator c = new calculator(8, 3); addition ad1 = (addition)c; console.writeline("addition={0}", ad1.add()); subtraction sub1 = (subtraction)c; console.writeline("subtraction={0}", sub1.sub()); multiplication mul1 = (multiplication)c; console.writeline("multiplication={0}", mul1.multiply()); division div1 = (division)c; console.writeline("division={0}", div1.divide()); console.readline(); } }

OUTPUT:-

47

1043006

PRACTICAL NO: -35 TITLE:- Write a program to show the implementation of unary operator. CODING:using system; class complex { private int x; private int y; public complex() { } public complex(int i,int j) { x = i; y = j; } public void showxy() { console.writeline("{0} {1}",x,y); } public static complex operator-(complex c) { complex temp=new complex(); temp.x=-c.x; temp.y=-c.y; return temp;

48

1043006 } } class prac41 { public static void main() { complex c1=new complex(10, 20); c1.showxy(); complex c2=new complex(); c2.showxy(); c2=-c1; c2.showxy(); console.readline(); } } OUTPUT:-

PRACTICAL NO: -36 TITLE:- Write a program to show how to use the ArrayList class. CODING:using System; using System.Collections; class MainClass { public static void Main() {

49

1043006 ArrayList myArrayList = new ArrayList(); myArrayList.Add("is"); myArrayList.Insert(1, "is"); string[] myStringArray = { "a", "test" }; myArrayList.AddRange(myStringArray); string[] anotherStringArray = { "here's", "some", "more", "text" }; myArrayList.InsertRange(myArrayList.Count, anotherStringArray); DisplayArrayList("myArrayList", myArrayList); Console.ReadLine(); } public static void DisplayArrayList(string arrayListName, ArrayList myArrayList) { for (int i = 0; i < myArrayList.Count; i++) { Console.WriteLine(arrayListName + "[" + i + "]=" + myArrayList[i]); } } } OUTPUT:-

PRACTICAL NO: -37 TITLE:- Write a program to create a class which can hold a complex number (X+iY) and using operator overloading perform the addition and subtraction of two complex numbers. CODING:using System; public struct Complex { public int real; public int imaginary;

50

1043006 public Complex(int real,int imaginary) { this.real=real; this.imaginary=imaginary; } public static Complex operator +(Complex c1,Complex c2) { return new Complex(c1.real+c2.real,c1.imaginary+c2.imaginary); } public static Complex operator -(Complex c1,Complex c2) { return new Complex(c1.real-c2.real,c1.imaginary-c2.imaginary); } public override string ToString() { return (String.Format("{0}+{1}i",real,imaginary)); } } class TestComplex { public static void Main() { Complex num1=new Complex(2,3); Complex num2=new Complex(3,4); Complex c=num1+num2; Console.WriteLine("First complex number: {0}",num1); Console.WriteLine("Second complex number: {0}",num2); Console.WriteLine("The sum of the two numbers:{0}",c); c=num1-num2; Console.WriteLine("The subtraction of two numbers:{0}",c); Console.Read(); } }

OUTPUT:-

51

1043006

PRACTICAL NO: -38 TITLE:- Define an interface that contains three methods (i) To calculate the square of the given number. (ii) to calculate cube of the given number. (iii) To calculate power m raised to n. Define a class Demo that implements this interface. Write a main method to check your code. CODING:using System; interface power { int square(int a); int cube(int b); double pows(double m, int n); } class Demo : power { public int square(int a) { return(a * a); } public int cube(int b) { return(b * b * b); } public double pows(double g, int n) {

52

1043006 for(int i=1;i<=n;i++) g=g *i; return(g); } } class PowerInt { static void Main(string[] args) { Demo dem = new Demo(); power pow; pow = dem as power; Console.WriteLine("Square of no. ={0}" ,pow.square(3)); Console.WriteLine("cube of no = {0}",pow.cube(7)); Console.WriteLine("power of no = {0:0.00}" , pow.pows(2.7,2)); } } OUTPUT:-

PRACTICAL NO: -39 TITLE:- Write a class Date that includes data members day, month and year and methods that could implement the following tasks. (i) Read a date from keyboard. (ii) Display a date (iii) Increment a date by one day. (iv) Compare two dates to see which is greater. Write a main method to check your code. 53

1043006

CODING:using System; class Date { static void Main(string[] args) { Console.WriteLine("Please enter the date dd/mm/yyyy"); string k= Console.ReadLine(); Console.WriteLine("First Date entered is {0}", k); string p= Console.ReadLine(); Console.WriteLine("Second Date entered is {0}", p); if(k.CompareTo(p)>0) Console.WriteLine("{0} is greater than {1} ", k, p); else Console.WriteLine("{0} is greater than {1} ", p, k); Console.ReadLine(); } } OUTPUT:-

PRACTICAL NO: -40 TITLE:- Given are two one dimensional arrays A and B which are in sorted order. Write a program to merge them into a single sorted array C that contains every item from arrays A and B in ascending order. CODING:-

54

1043006 using System; class arrayMerge { static void Main(string[] args) { int i,j,k; int[] A={14,32,53,114,53}; int[] B={60,67,88,79,170}; int[] C=new int[10]; for(i=0;i<A.Length;i++) { C[i]=A[i]; } for(j=i,k=0;j<(i+B.Length);j++,k++) { C[j]=B[k]; } Array.Sort(C); for(int h=0;h<C.Length;h++) Console.Write(C[h] + " "); } } OUTPUT:-

PRACTICAL NO: -41 TITLE:- Create a class named Integer to hold two integer values. Design a method Swap that could take two integer objects as parameters and swap their contents. Write a main program to implement class Integer and method Swap. CODING:using System; class Integer1 { 55

1043006 int g; int h; Integer1(int i1, int i2) { this.g = i1; this.h = i2; } static void swap(Integer1 x, Integer1 y) { int temp1, temp2; temp1 = x.g; temp2 = x.h; x.g=y.g; x.h=y.h; y.g=temp1; y.h=temp2; } void display() { Console.WriteLine(g + " and " + h); } static void Main(string[] args) { Integer1 t1 = new Integer1(5,7); Integer1 t2 = new Integer1(9,12); t1.display(); t2.display(); swap(t1, t2); t1.display(); t2.display(); } }

OUTPUT:-

56

1043006

PRACTICAL NO: -42 TITLE:- Define an exception called NoMatchException that is thrown when a string is not equal to TYBSCIT. Write a program that uses this exception. CODING:using System; class NoMatchException : System.Exception { public static void Main(string[] args) { string str = Console.ReadLine(); try { if(!(str.Equals("TYBSCIT"))) { throw new NoMatchException(); } else Console.WriteLine("You entered the string {0} ",str); } catch (NoMatchException noMExcep) { Console.WriteLine("Exception is caught"); { Console.WriteLine("Exception message"+noMExcep.Message); Console.WriteLine("Not entered TYBSCIT instead entered string{0}",str); } } 57

1043006 } } OUTPUT:-

58

S-ar putea să vă placă și