c# program (1)

71
1043006 C# PRACTICAL C# PRACTICAL PRACTICAL NO: - 01 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:- 1

Upload: vijay-nayak

Post on 14-Oct-2014

2.380 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: c# PROGRAM (1)

1043006

C# PRACTICALC# PRACTICAL

PRACTICAL NO: - 01 PRACTICAL NO: - 01

TITLE:- Write a program to perform following taska. Declare two variable x and y at float types variables.b. Declare m as an integer variablec. Assign the value 75.86 to x and 143.48 yd. Assign the sum of x and y to me. Display the value of mf. 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:-

1

Page 2: c# PROGRAM (1)

1043006

PRACTICAL NO: - 02 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 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.

2

Page 3: c# PROGRAM (1)

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 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.

3

Page 4: c# PROGRAM (1)

1043006

CODING:A) Expression x-y=10using 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;

4

Page 5: c# PROGRAM (1)

1043006

r=x-(y=100); Console.WriteLine("The value is :" +r); }}

OUTPUT:

PRACTICAL NO: - 05 PRACTICAL NO: - 05

TITLE:- Given the radius of 12.5cm.Write a program to compute it’s 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); }

5

Page 6: c# PROGRAM (1)

1043006

}

OUTPUT :

PRACTICAL NO: - 06 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;

6

Page 7: c# PROGRAM (1)

1043006

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

OUTPUT:

PRACTICAL NO: - 07 PRACTICAL NO: - 07

TITLE:- Admission for professional course is subject to the following conditiona) Marks in mathematics >= 60b) Marks in Physics >=50c) Marks in chemistry >= 40d) Marks in all subject >= 200

ORe) 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());

7

Page 8: c# PROGRAM (1)

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 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

Page 9: c# PROGRAM (1)

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 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 statementusing System;class nestedif{ public static void Main() { int x,y;

9

Page 10: c# PROGRAM (1)

1043006

Console.WriteLine("Enter the value of x: "); 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)

10

Page 11: c# PROGRAM (1)

1043006

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

Using conditional operatorCODING: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

Page 12: c# PROGRAM (1)

1043006

PRACTICAL NO: - 10 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); } }

12

Page 13: c# PROGRAM (1)

1043006

OUTPUT:

PRACTICAL NO: -11 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;

13

Page 14: c# PROGRAM (1)

1043006

b=c; Console.Write(c+ " "); i++; } while(i<(x-2)); } }

OUTPUT:

PRACTICAL NO: -12 PRACTICAL NO: -12

TITLE:- Write a Program to print the following outputs using for loopsa) 1 c) 1 b)$ $ $ $

2 2 2 2 $ $ $3 3 3 3 3 3 $ $4 4 4 4 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++) {

14

Page 15: c# PROGRAM (1)

1043006

for(int j=1;j<=i;j++) { 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

Page 16: c# PROGRAM (1)

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

Page 17: c# PROGRAM (1)

1043006

}

OUTPUT:

PRACTICAL NO: -13 PRACTICAL NO: -13

TITLE:- Demonstrate the typical use of the following jump statement.a) breakb) continuec) 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

Page 18: c# PROGRAM (1)

1043006

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

OUTPUT:

PRACTICAL NO: -14 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;

18

Page 19: c# PROGRAM (1)

1043006

num=num/10; } Console.WriteLine("The reverse of the number is :"+rev); } }

OUTPUT:

PRACTICAL NO: -15 PRACTICAL NO: -15

TITLE:- Write a program to print the floyd’s triangle.12 34 5 67 8 9 10………..79…………….91

CODING:- using System;class umat{ public static void Main() { int num;

19

Page 20: c# PROGRAM (1)

1043006

int k=1; 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 PRACTICAL NO: -16

TITLE:- Write a program to print the following output.10 11 0 10 1 0 11 0 1 0 1Ask the number of rows from the user.

20

Page 21: c# PROGRAM (1)

1043006

CODING: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(); } }}

21

Page 22: c# PROGRAM (1)

1043006

OUTPUT :

PRACTICAL NO: -17 PRACTICAL NO: -17

TITLE:- Write a program to print the following output.1 0 00 1 00 0 1Ask 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++) {

22

Page 23: c# PROGRAM (1)

1043006

for(int j=1;j<=(i-1);j++) { 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 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) {

23

Page 24: c# PROGRAM (1)

1043006

int sum=0; 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]; }

24

Page 25: c# PROGRAM (1)

1043006

double m=obj.mean(x); Console.WriteLine("The mean is :" +m); obj.standard(fx,n,m); } }

OUTPUT:

PRACTICAL NO: -19 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

Page 26: c# PROGRAM (1)

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();

}}

26

Page 27: c# PROGRAM (1)

1043006

OUTPUT:

PRACTICAL NO: -20 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

Page 28: c# PROGRAM (1)

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 PRACTICAL NO: -21

28

Page 29: c# PROGRAM (1)

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

Page 30: c# PROGRAM (1)

1043006

PRACTICAL NO: -22 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

Page 31: c# PROGRAM (1)

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 PRACTICAL NO: -23

31

Page 32: c# PROGRAM (1)

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 PRACTICAL NO: -24

32

Page 33: c# PROGRAM (1)

1043006

TITLE:- Write a method that takes 3 values as input parameter andReturns 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

Page 34: c# PROGRAM (1)

1043006

PRACTICAL NO: -25 PRACTICAL NO: -25

TITLE:- Write a method that takes 3 values as input parameter andReturns 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:");

34

Page 35: c# PROGRAM (1)

1043006

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 Largest number is :"+result);

}}

OUTPUT:

PRACTICAL NO: -26 PRACTICAL NO: -26

TITLE:- Write a method that takes an array as an input parameter uses 2 methods a) To find largest array elementb) To compute the average of array element.

CODING:using System;

class edit{ public void avgmax(params int[] z)

{

35

Page 36: c# PROGRAM (1)

1043006

int max=z[0]; 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

Page 37: c# PROGRAM (1)

1043006

PRACTICAL NO: -27 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

Page 38: c# PROGRAM (1)

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 PRACTICAL NO: -28

TITLE:-TITLE:- Create a class named Double. This class contains three double numbers. 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. Overload +,-,*,/ operators so that they can be applied to the objects of the class Double. Write a c# program to test it.Write a c# program to test it.

CODINGCODING:- :- using System;

class Double

38

Page 39: c# PROGRAM (1)

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

Page 40: c# PROGRAM (1)

1043006

OUTPUT:-

PRACTICAL NO: -29 PRACTICAL NO: -29 TITLE:-TITLE:- Create a class example to hold one integer value. Include a constructor to display 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 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 destroyed”. Write a c# program to demonstrate the creation and destruction of objects of example class.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);

40

Page 41: c# PROGRAM (1)

1043006

z.x();Console.ReadLine();

}}

OUTPUT:-

PRACTICAL NO: -30 PRACTICAL NO: -30

TITLE:-TITLE:- Write a c# program that throws NumberNotInRange exception if number entered Write a c# program that throws NumberNotInRange exception if number entered by user is not in the range 1 to 100.by user is not in the range 1 to 100.

CODING:-CODING:-using System;using System;class NumberNotInRange : System.Exceptionclass NumberNotInRange : System.Exception{{public static void Main(string[] args)public static void Main(string[] args){{

int no = int.Parse(args[0]);int no = int.Parse(args[0]);trytry{{

if(no<1 || no>100)if(no<1 || no>100){{

throw new NumberNotInRange();throw new NumberNotInRange();}}

}}catch(NumberNotInRange noExcep)catch(NumberNotInRange noExcep){{

Console.WriteLine("Exception is caught");Console.WriteLine("Exception is caught");Console.WriteLine("Exception message " + noExcep.Message);Console.WriteLine("Exception message " + noExcep.Message);

}}

41

Page 42: c# PROGRAM (1)

1043006

}}} }

OUTPUT:-OUTPUT:-

PRACTICAL NO: -31 PRACTICAL NO: -31

TITLE:-TITLE:- Write a program to convert the temperature in Fahrenheit(starting from 98.5 to 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 102 in steps of 0.1) to Celsius using the formula C=(F-32)/1.8 and display the values in a tabular form.tabular form.

CODING:-CODING:-using System;using System;

class FahrenCelciusclass FahrenCelcius{{

static void Main(string[] args)static void Main(string[] args){{

double far,cel;double far,cel;Console.WriteLine("Fahrenheit Celcius");Console.WriteLine("Fahrenheit Celcius");for(far = 98.5;far <= 102.0;far = far+0.1)for(far = 98.5;far <= 102.0;far = far+0.1){{

cel = (far - 32) / 1.8;cel = (far - 32) / 1.8;Console.WriteLine("{0,20}\t{1,20}",far, cel);Console.WriteLine("{0,20}\t{1,20}",far, cel);

}}}}

}}

42

Page 43: c# PROGRAM (1)

1043006

OUTPUT:-OUTPUT:-

PRACTICAL NO: -32 PRACTICAL NO: -32

TITLE:-TITLE:- Write a program that Prints the following formWrite a program that Prints the following form * * * * * * * * * *

* * * * * * *

* * *

CODING:-using system;class asterikprint{

static void main(string[] args)

43

Page 44: c# PROGRAM (1)

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 PRACTICAL NO: -33

TITLE:-TITLE:- Given a list of marks ranging from 0 to 100,write a program to 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.compute and print the number of students who have obtained marks.

(a)(a) In the range 81 to 100In the range 81 to 100(b)(b) In the range 61 to 80In the range 61 to 80(c)(c) In the range 41 to 60, andIn the range 41 to 60, and(d)(d) In the range 0 to 40In the range 0 to 40

CODING:-CODING:-

44

Page 45: c# PROGRAM (1)

1043006

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

}}

OUTPUT:-OUTPUT:-

45

Page 46: c# PROGRAM (1)

1043006

PRACTICAL NO: -34 PRACTICAL NO: -34

TITLE:-TITLE:- Write a program to show the implementation of multiple interfaces.Write a program to show the implementation of multiple interfaces.

CODING:-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

Page 47: c# PROGRAM (1)

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

Page 48: c# PROGRAM (1)

1043006

PRACTICAL NO: -35 PRACTICAL NO: -35

TITLE:TITLE: - - Write a program to show the implementation of unary operator.Write a program to show the implementation of unary operator.

CODING:-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

Page 49: c# PROGRAM (1)

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 PRACTICAL NO: -36

TITLE:TITLE: - - Write a program to show how to use the ArrayList class.Write a program to show how to use the ArrayList class.

CODING:-CODING:-using System;using System;using System.Collections;using System.Collections;class MainClassclass MainClass{{ public static void Main() public static void Main() { {

49

Page 50: c# PROGRAM (1)

1043006

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

PRACTICAL NO: -37 PRACTICAL NO: -37 TITLE:-TITLE:- Write a program to create a class which can hold a complex number (X+iY) and 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.using operator overloading perform the addition and subtraction of two complex numbers.

CODING:-CODING:-using System;using System;public struct Complexpublic struct Complex{{ public int real; public int real; public int imaginary; public int imaginary;

50

Page 51: c# PROGRAM (1)

1043006

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

OUTPUT:-OUTPUT:-

51

Page 52: c# PROGRAM (1)

1043006

PRACTICAL NO: -38 PRACTICAL NO: -38

TITLE:TITLE: - - Define an interface that contains three methodsDefine 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

Page 53: c# PROGRAM (1)

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 PRACTICAL NO: -39

TITLE:-TITLE:- Write a class Date that includes data members day, month and year and methodsWrite a class Date that includes data members day, month and year and methods that could implement the following tasks.that could implement the following tasks.

(i)(i) Read a date from keyboard.Read a date from keyboard.

(ii)(ii) Display a dateDisplay a date

(iii)(iii) Increment a date by one day.Increment a date by one day.

(iv)(iv) Compare two dates to see which is greater.Compare two dates to see which is greater.Write a main method to check your code.Write a main method to check your code.

53

Page 54: c# PROGRAM (1)

1043006

CODING:-CODING:-

using System;using System;class Dateclass Date{{static void Main(string[] args)static void Main(string[] args){{

Console.WriteLine("Please enter the date dd/mm/yyyy");Console.WriteLine("Please enter the date dd/mm/yyyy");string k= Console.ReadLine();string k= Console.ReadLine();Console.WriteLine("First Date entered is {0}", k);Console.WriteLine("First Date entered is {0}", k);string p= Console.ReadLine();string p= Console.ReadLine();Console.WriteLine("Second Date entered is {0}", p);Console.WriteLine("Second Date entered is {0}", p);if(k.CompareTo(p)>0)if(k.CompareTo(p)>0)Console.WriteLine("{0} is greater than {1} ", k, p);Console.WriteLine("{0} is greater than {1} ", k, p);elseelseConsole.WriteLine("{0} is greater than {1} ", p, k);Console.WriteLine("{0} is greater than {1} ", p, k);Console.ReadLine();Console.ReadLine();

}}}}

OUTPUT:-OUTPUT:-

PRACTICAL NO: -40 PRACTICAL NO: -40

TITLE:-TITLE:- Given are two one dimensional arrays ‘A’ and ‘B’ which are in sorted order. 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 Write a program to merge them into a single sorted array ‘C’ that contains every item from arrays ‘A’ and ‘ B’ in ascending order.arrays ‘A’ and ‘ B’ in ascending order.

CODING:-CODING:-

54

Page 55: c# PROGRAM (1)

1043006

using System;using System;class arrayMergeclass arrayMerge{{

static void Main(string[] args)static void Main(string[] args){{

int i,j,k;int i,j,k;int[] A={14,32,53,114,53};int[] A={14,32,53,114,53};int[] B={60,67,88,79,170};int[] B={60,67,88,79,170};int[] C=new int[10];int[] C=new int[10];for(i=0;i<A.Length;i++)for(i=0;i<A.Length;i++){{

C[i]=A[i];C[i]=A[i];}}for(j=i,k=0;j<(i+B.Length);j++,k++)for(j=i,k=0;j<(i+B.Length);j++,k++){{

C[j]=B[k];C[j]=B[k];}}Array.Sort(C);Array.Sort(C);for(int h=0;h<C.Length;h++)for(int h=0;h<C.Length;h++)Console.Write(C[h] + " ");Console.Write(C[h] + " ");

}}}}OUTPUT:-OUTPUT:-

PRACTICAL NO: -41 PRACTICAL NO: -41

TITLE:-TITLE:- Create a class named ‘Integer’ to hold two integer values. Design a method ‘Swap’ 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 that could take two integer objects as parameters and swap their contents. Write a main program to implement class Integer and method Swap.program to implement class Integer and method Swap.

CODING:-CODING:-using System;class Integer1

55

Page 56: c# PROGRAM (1)

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

Page 57: c# PROGRAM (1)

1043006

PRACTICAL NO: -42 PRACTICAL NO: -42

TITLE:-TITLE:- Define an exception called “NoMatchException” that is thrown when a string Define an exception called “NoMatchException” that is thrown when a string is not equal to “TYBSCIT”. Write a program that uses this exception.is not equal to “TYBSCIT”. Write a program that uses this exception.

CODING:- using System; using System; class NoMatchException : System.Exception class NoMatchException : System.Exception{{public static void Main(string[] args)public static void Main(string[] args){{string str = Console.ReadLine();string str = Console.ReadLine();trytry{{

if(!(str.Equals("TYBSCIT")))if(!(str.Equals("TYBSCIT"))){{

throw new NoMatchException();throw new NoMatchException();}}elseelse

Console.WriteLine("You entered the string {0} ",str);Console.WriteLine("You entered the string {0} ",str); } } catch (NoMatchException noMExcep) catch (NoMatchException noMExcep) { {

Console.WriteLine("Exception is caught");Console.WriteLine("Exception is caught");{{

Console.WriteLine("Exception message"+noMExcep.Message);Console.WriteLine("Exception message"+noMExcep.Message);Console.WriteLine("Not entered TYBSCIT instead entered string{0}",str);Console.WriteLine("Not entered TYBSCIT instead entered string{0}",str);

}}}}

}}

57

Page 58: c# PROGRAM (1)

1043006

}}

OUTPUT:-OUTPUT:-

58