You are on page 1of 33

Practical No: 01 Write a program that would illustrate concept of declaration and initialization of value type variables.

Use the following literals for initializing the variables. A 50 123456789 1234567654321 True 0.000000345 1.23e.5

using System; class ValueTypeDemo { public static void Main() { char c = 'A'; byte b = 50; int i = 123456789; long l = 123456754321; bool bl = true; double d = 0.000000345; float g = 1.23e5f; Console.WriteLine("The charecter value type variable holds " + c + " value"); Console.WriteLine("The byte value type variable holds " + b + " value"); Console.WriteLine("The integer value type variable holds " + i + " value"); Console.WriteLine("The long value type variable holds " + l + " value"); Console.WriteLine("The boolean value type variable holds " + bl + " value"); Console.WriteLine("The double value type variable holds " + d + " value"); Console.WriteLine("The float value type variable holds " + g + " value"); Console.ReadLine(); } } Output: The charecter value type variable holds A value The byte value type variable holds 50 value The integer value type variable holds 123456789 value The long value type variable holds 123456754321value The boolean value type variable holds True value The double value type variable holds 3.34E-07 value The float value type variable holds 123000 value

Practical No: 02 Write a program to read interactively two integer & two double values using methods Console.ReadLine() , int.Parse() and double.Parse() methods and then display their Sum Product Difference Integer division Modulus division

using System; class Program { public static void Main(string[] args) { int x, y; float a, b; Console.WriteLine("Enter two integer numbers"); x = int.Parse(Console.ReadLine()); y = int.Parse(Console.ReadLine()); Console.WriteLine("Enter two float numbers"); a = float.Parse(Console.ReadLine()); b = float.Parse(Console.ReadLine()); Console.WriteLine("The sum of two integer numbers is " + (x + y)); Console.WriteLine("The difference of two integer numbers is " + (x - y)); Console.WriteLine("The product of two integer numbers is " + (x * y)); Console.WriteLine("The division of two integer numbers is " + (x / y)); Console.WriteLine("The remainder of two integer numbers is " + (x % y)); Console.WriteLine("*************************************************** ********"); Console.WriteLine("The sum of two float numbers is " + (a + b)); Console.WriteLine("The difference of two float numbers is " + (a - b)); Console.WriteLine("The product of two float numbers is " + (a * b)); Console.WriteLine("The division of two float numbers is " + (a / b)); Console.WriteLine("The remainder of two float numbers is " + (a % b)); Console.ReadLine(); } }

Output: Enter two integer numbers 2 3 Enter two float numbers 2.5 3.5 The sum of two integer numbers is 5 The difference of two integer numbers is -1 The product of two integer numbers is 6 The division of two integer numbers is 0 The remainder of two integer numbers is 2 *********************************************************** The sum of two float numbers is 6 The difference of two float numbers is -1 The product of two float numbers is 8.75 The division of two float numbers is 0.7142857 The remainder of two float numbers is 2.5

Practical No: 03 Write a program to convert the given temperature in Fahrenheit to Celsius using the following conversion formula C= (F-32)/1.8 using System; class Temperature { public static void Main() { float fahrenheit,celcius; string s; Console.Write("Enter the temperature in fahrenheit : "); s = Console.ReadLine(); fahrenheit = float.Parse(s); celcius = (float)((fahrenheit-32)/1.8); Console.WriteLine("The Temperature in celcius = " +celcius); Console.ReadLine(); } } Output: Enter the temperature in fahrenheit : 98 Temperature in celcius = 36.66667

Practical No: 04 Admission to a professional course is subject to the following conditions: Marks in mathematics >= 60 Marks in physics >= 50 Marks in chemistry >= 40 Total in all three subjects >= 200 (Or) Total in mathematics and physics>=150

Given the marks in the three subjects, write a program to process the application to list the eligible candidates. using System; class Admission { public static void Main() { float mksMaths, mksPhysics, mksChemistry, mksTotal, MathsPhysics; int response; beginning: Console.WriteLine(""); Console.WriteLine(" ********** Students Enrollment Checking Criteria ********** "); Console.WriteLine(""); Console.Write("Enter the marks in Maths : "); mksMaths = float.Parse(Console.ReadLine()); Console.Write("Enter the marks in Chemistry : "); mksChemistry = float.Parse(Console.ReadLine()); Console.Write("Enter the marks in Physics : "); mksPhysics = float.Parse(Console.ReadLine()); mksTotal = (float)(mksMaths + mksChemistry + mksPhysics); MathsPhysics = (float)(mksMaths + mksPhysics); if ((mksMaths >= 60 && mksPhysics >= 50 && mksChemistry >= 40) || (mksTotal >=200 || (mksMaths + mksPhysics) >= 150)) { Console.WriteLine("Congratulations !!! The candidate is selected ... "); } else {

Console.WriteLine("Sorry, The candidate is rejected ... Better luck for next year."); } Console.WriteLine(""); Console.Write("Enter 1 for next candidate, 0 to exit : "); response = int.Parse(Console.ReadLine()); if (response == 1) goto beginning; else goto end; end: Console.ReadLine(); } } Output: ********** Students Enrollment Checking Criteria ********** Enter the marks in Maths : 50 Enter the marks in Chemistry: 40 Enter the marks in Physics: 35 Sorry, The candidate is rejected ... Better luck for next year. Enter 1 for next candidate, 0 to exit: 1 ********** Students Enrollment Checking Criteria ********** Enter the marks in Maths : 70 Enter the marks in Chemistry: 80 Enter the marks in Physics: 85 Congratulations!!! The candidate is selected... Enter 1 for next candidate, 0 to exit: 0

Practical No: 05 Write a program that will read the value of x and evaluate the following function {1 for x>0 Y= {0 for x=0 {-1 for x<0 Using Nested if statements Else if statements, and Conditional operator? using System; class ChangingValuesOfY { public static void Main() { int x,y; Console.Write("Enter the value of x : "); x = int.Parse(Console.ReadLine()); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine(" ********* Changing values of Y by nested if statements *********"); Console.WriteLine(""); if (x != 0) { if (x > 0) { Console.WriteLine("Y = 1"); } if (x < 0) { Console.WriteLine("Y = -1"); } } else { Console.WriteLine("Y = 0"); } Console.WriteLine(""); Console.WriteLine(" ********* Changing values of Y by else if statements *********"); Console.WriteLine(""); if (x == 0) {

Console.WriteLine("Y = 0"); } else if(x > 0) { Console.WriteLine("Y = 1"); } else { Console.WriteLine("Y = -1"); } Console.WriteLine(""); Console.WriteLine(" ********* Changing values of Y by conditional operator *********"); Console.WriteLine(""); y = (x != 0)?((x>0)?1:-1):0; Console.WriteLine("Y = "+y); Console.ReadLine(); } } Output: Enter the value of x : 2 ********* Changing values of Y by nested if statements ********* Y=1 ********* Changing values of Y by else if statements ********* Y=1 ********* Changing values of Y by conditional operator ********* Y=1

Practical No: 06(a) Write a program to draw following output using System; class Pattern { public static void Main() { int i, j, num = 5, k; for (i = 5; i >= 1; i--) { for (k = num; k >= i; k--) { Console.Write(" "); } for (j = 1; j <= i; j++) { Console.Write("#"); } Console.WriteLine(); } Console.ReadLine(); } } Output: ##### #### ### ## #

Practical No: 06(b) Write a program to draw following output using System; class FloydsTriangle1 { public static void Main() { int i,j,k=1; for (i=1; i<=4; i++) { for (j=1; j<i+1; j++) { Console.Write(k++ + " "); } Console.Write("\n"); } Console.ReadLine(); } } Output: 1 23 456 7 8 9 10

Practical No: 06(c) Write a program to draw following output using System; class PyramidNumbers { public static void Main() { int i,j,num=5,k; for(i=1;i<=num;i++) { for(k=num;k>=i;k--) { Console.Write(" "); } for(j=1;j<=i;j++) { Console.Write(" " +i); } Console.Write("\n"); } Console.ReadLine(); } } Output: 1 2 3 4 5 5 4 5 3 4 5 2 3 4 5

Practical No: 06(d) Write a program to draw following output using System; class pascalTriangle { public static void Main() { int binom = 1, q = 0, r, x; Console.WriteLine("Enter the number of rows"); string s = Console.ReadLine(); int p = Int32.Parse(s); Console.WriteLine(p); Console.WriteLine("The Pascal Triangle"); while (q < p) { for (r = 40 - (3 * q); r > 0; --r) Console.Write(""); for (x = 0; x <= q; ++x) { if ((x == 0) || (q == 0)) binom = 1; else binom = (binom * (q - x + 1)) / x; Console.Write(binom); } Console.Write("\n"); ++q; } Console.ReadLine(); } } Output: Enter the number of rows 5 The Pascal Triangle 1 11 121 1331 14641

Practical No: 07 Write a method that would calculate the values of money for the given period of years and print the results giving the following details on one time of output: Principle amount Interest rate Period in years Final value

The method takes principle amount, Interest rate and Period as input parameters and does not return any value. The following formula may be used respectively: Value of the end of year = Value at start of year (1+interest rate) Write a program to test the method using System; class Program { public void calculate() { decimal p, n, r, amt, val; Console.WriteLine("Enter the Principle Amount "); p = decimal.Parse(Console.ReadLine()); Console.WriteLine("Enter the number of year "); n = decimal.Parse(Console.ReadLine()); Console.WriteLine("Enter the rate of interest "); r = decimal.Parse(Console.ReadLine()); amt = (p * n * r) / 100; Console.WriteLine("The interest amount is " + amt); val = p * (1 + r); Console.WriteLine("The value at the end of year is " + val); } } class test { public static void Main() { Program p = new Program(); p.calculate(); Console.ReadLine();

} }

Output: Enter the Principle Amount 1000 Enter the number of years 2 Enter the rate of interest 10 The interest amount is 200 The value at the end of the year is 11000

Practical No: 08 Write a method that takes an array as an input parameter and uses two methods, one to find the largest array element and other to compute the average of array elements. using System; class ArrayFunction { public static void Main() { int Largest; double Average; int c; int num; int[] array1; Console.Write("Enter the number of Elements in an Array : "); c = Int32.Parse(Console.ReadLine()); array1 = new int[c]; for (int i = 0; i < c; i++) { Console.WriteLine("Enter the element " + i); num = Int32.Parse(Console.ReadLine()); array1[i] = num; } foreach (int i in array1) { Console.Write(" " + i); } Console.WriteLine(); Largest = Large(array1); Average = Avg(array1); Console.WriteLine("\n The largest element in the array is " + Largest); Console.WriteLine("The Average of elements in the array is " + Average); Console.ReadLine(); } // Determining the largest array element static int Large(params int[] arr) { int temp = 0; for (int i = 0; i < arr.Length; i++) { if (temp <= arr[i]) { temp = arr[i]; }

} return (temp); } // Determining the average of array elements static double Avg(params int[] arr) { double sum = 0; for (int i = 0; i < arr.Length; i++) { sum = sum + arr[i]; } sum = sum / arr.Length; return (sum); } } Output: Enter the number of Elements in an Array : 3 Enter the element 0 3 Enter the element 1 4 Enter the element 2 5 345 The largest element in the array is 5 The Average of elements in the array is 4

Practical No: 09 Write a C# program that uses an array of five items of data type designed in

using System; using System.Collections.Generic; using System.Linq; using System.Text; class program { struct student { public int code; public string name; public int cost; public int qty; public int totalitems; } public static void Main() { student[] s1 = new student[5]; for (int i = 1; i <= 5; i++) { Console.WriteLine("Enter the " +i +" item's code"); s1[i].code = int.Parse(Console.ReadLine()); Console.WriteLine("Enter the " +i +" item's name"); s1[i].name = Console.ReadLine(); Console.WriteLine("Enter the " +i +" item's cost"); s1[i].cost = int.Parse(Console.ReadLine()); Console.WriteLine("Enter the " +i +" item's qty"); s1[i].qty = int.Parse(Console.ReadLine()); s1[i].totalitems = s1[i].cost * s1[i].qty; } Console.WriteLine("Code\tName\tCost\tqty\tTotal_Items"); for (int i = 0; i < 5; i++) { Console.WriteLine(s1[i].code + "\t" + s1[i].name + "\t" + s1[i].cost + "\t" + s1[i].qty + "\t" + s1[i].totalitems); } Console.ReadLine(); } }

Output: Enter the 1 item's code 235 Enter the 1 item's name Fan Enter the 1 item's cost 350 Enter the 1 item's qty 5 Enter the 2 item's code 236 Enter the 2 item's name Trolley Enter the 2 item's cost 250 Enter the 2 item's qty 3 Enter the 3 item's code 237 Enter the 3 item's name Washing-Machine Enter the 3 item's cost 5000 Enter the 3 item's qty 1 Enter the 4 item's code 238 Enter the 4 item's name Air-Conditioner Enter the 4 item's cost 20000 Enter the 4 item's qty 2 Enter the 5 item's code 239 Enter the 5 item's name Pen-Drive Enter the 5 item's cost 750 Enter the 5 item's qty 10

Code 235 236 237 238 239

Name

Cost

Qty

Total_Items 1750 750 5000 40000 7500

Fan 350 5 Trolley 250 3 Washing-Machine 5000 1 Air-Conditioner 20000 2 Pen-Drive 750 10

Practical No: 10 Design a class to represent a bank account. Include the following members: Data members: Name of the depositor Account number Type of account Balance amount in the account Members: To assign initial values To deposit an amount To withdraw an amount after checking balance To display the name and balance

using System; class Account { public string Name; public int Account_No; public string Account_Type; public double Balance; public double D_amt; public double W_amt; public void Initial_info() { Console.WriteLine("Enter the name of depositor"); Name = Console.ReadLine(); Console.WriteLine("Enter Account Number"); Account_No = int.Parse(Console.ReadLine()); Console.WriteLine("Enter Type of Account"); Account_Type = Console.ReadLine(); Console.WriteLine("Enter initial Balance"); Balance = double.Parse(Console.ReadLine()); } public void Deposite() { Console.WriteLine("Enter the amount to be deposited"); D_amt = double.Parse(Console.ReadLine()); Balance = Balance + D_amt; }

public void Withdraw() { Console.WriteLine("The Balance is " + Balance); Console.WriteLine("Enter the amount to withdraw"); W_amt = double.Parse(Console.ReadLine()); Balance = Balance - W_amt; } public void display() { Console.WriteLine("The Name of the Account Holder is " + Name); Console.WriteLine("The Total balance is " + Balance); } } class Test { static void Main() { Account acct = new Account(); acct.Initial_info(); String c = "Y"; do { Console.WriteLine("****MENU****"); Console.WriteLine("1:Deposite"); Console.WriteLine("2:Withdraw"); Console.WriteLine("3:Balance Enquiry"); Console.WriteLine("Enter Your Choice"); int ch = int.Parse(Console.ReadLine()); switch (ch) { case 1: Console.Clear(); acct.Deposite(); break; case 2: Console.Clear(); acct.Withdraw(); break; case 3: Console.Clear(); acct.display(); break; }

Console.Clear(); Console.WriteLine("Do You Wnat to continue"); c = Console.ReadLine(); } while ((c == "Y") || (c == "N")); Console.ReadLine(); } } Output: Enter the name of depositor Abc Enter the Account Number 12345 Enter the Type of Account Saving Enter initial balance 40000 ****MENU**** 1; deposit 2: Withdraw 3: Balance Enquiry Enter your choice 1 Enter Amount to be deposited 4000 Do you want to continue? N

Practical No: 11 Create base class called Shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specialized classes called triangle and rectangle from the base Shape. Add to the base class, a method Set to initialize base class data members and another method Area to compute and display the area of figures. Make Area as virtual method and redefine this method suitable in the derived class. Using these classes, design a program that will accept dimensions of a triangle or rectangle interactively and display the area.

using System; using System.Collections.Generic; using System.Linq; using System.Text; class Shape { public double a; public double b; public void Set(double x, double y) { Console.WriteLine("Enter 2 values: "); x = double.Parse(Console.ReadLine()); y= double.Parse(Console.ReadLine()); a = x; b = y; } public virtual void Area() { Console.WriteLine("Area"); } class Triangle : Shape { public override void Area() { Console.WriteLine("The Area of Triangle is " + (0.5 * a * b)); } } class Rectangle : Shape { public override void Area() { Console.WriteLine("The Area of Rectangle is " + a * b);

} } class Test { public static void Main() { Shape sh = new Shape(); sh = new Triangle(); sh.Set(0, 0); sh.Area(); sh = new Rectangle(); sh.Set(0, 0); sh.Area(); Console.ReadLine(); } } }

Output:

Enter 2 values: 4 5 The Area of Triangle is 10 Enter 2 values: 3 6 The Area of Rectangle is 18

Program No: 12 Create a class name double this class contain three double number overload +,-,*, / operator so that they can be applied to the objects of class double. Write C# program to test it using System; class OperatorOverload { double x, y; public OperatorOverload() { } public OperatorOverload(double a, double b) { x = a; y = b; } public static OperatorOverload operator +(OperatorOverload o1, OperatorOverload o2) { OperatorOverload o3 = new OperatorOverload(); o3.x = o1.x + o2.x; o3.y = o1.y + o2.y; return o3; } public static OperatorOverload operator -(OperatorOverload o1, OperatorOverload o2) { OperatorOverload o3 = new OperatorOverload(); o3.x = o1.x - o2.x; o3.y = o1.y - o2.y; return o3; } public static OperatorOverload operator *(OperatorOverload o1, OperatorOverload o2) { OperatorOverload o3 = new OperatorOverload(); o3.x = o1.x * o2.x; o3.y = o1.y * o2.y; return o3; } public static OperatorOverload operator /(OperatorOverload o1, OperatorOverload o2) { OperatorOverload o3 = new OperatorOverload(); o3.x = o1.x / o2.x; o3.y = o1.y / o2.y; return o3; } public void display() {

Console.WriteLine("x=" + x); Console.WriteLine("y=" + y); } public static void Main(string[] args) { OperatorOverload op1, op2, add, sub, mul, div; op1 = new OperatorOverload(1.1, 1.2); op2 = new OperatorOverload(1.3, 1.4); Console.WriteLine("Values of object1="); op1.display(); Console.WriteLine("Values of object2="); op2.display(); add = op1 + op2; Console.WriteLine("After overloading + operator values are="); add.display(); sub = op1 - op2; Console.WriteLine("After overloading - operator values are="); sub.display(); mul = op1 * op2; Console.WriteLine("After overloading * operator values are="); mul.display(); div = op1 / op2; Console.WriteLine("After overloading / operator values are="); div.display(); Console.ReadLine(); } }

Output: Values of object1= x=1.1 y=1.2 Values of object2= x=1.3 y=1.4 After overloading + operator values are= x=2.4 y=2.6 After overloading - operator values are= x=-0.2 y=-0.2 After overloading * operator values are= x=1.43 y=1.68 After overloading / operator values are= x=0.846153846153846 y=0.857142857142857

Practical No: 13 Write a program to create a delegate and event using System; using System.Collections.Generic; using System.Linq; using System.Text; public delegate void EDelegate(string str); class EventClass { //declaration of events public event EDelegate Status; public void TriggerEvent() { if(Status!=null ) Status ("Event Triggered"); } } class EventTest { public static void Main(string[] args) { EventClass ec=new EventClass (); EventTest et = new EventTest(); ec.Status +=new EDelegate(et.EventCatch); ec.TriggerEvent (); Console.ReadLine(); } public void EventCatch(string str) { Console.WriteLine (str); } }

Output: Event Triggered

Practical No: 14 Write a program to print the integer 3456789 using the following format specifications: a) b) c) d) e) Using the format Character D Using the format Character N Using the format Character E Using the Zero placeholder Using the pound Character

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace formatnumber { public class DecimalPoint { public static void Main() {

Console.WriteLine("Integer format"); Console.WriteLine("{0:D}", 3456789); Console.WriteLine(); Console.WriteLine("Numbar format"); Console.WriteLine("{0:N}",3456789); Console.WriteLine(); Console.WriteLine("Exponential Form"); Console.WriteLine("{0:###.000E-00}", 3456789); Console.WriteLine(); Console.WriteLine("Zero place holder"); Console.WriteLine("{0:000}", 3456789); Console.WriteLine();

Console.WriteLine("Pound character"); Console.WriteLine("{0:#####.000}",3456789); Console.WriteLine(); Console.WriteLine("Comma"); Console.WriteLine("{0:#,###.##}",3456789); Console.ReadLine(); Console.WriteLine();

} } }

Output:

Integer format 3456789 Numbar format 3,456,789.00 Exponential Form 345.679E04 Zero place holder 3456789 Pound character 3456789.000 Comma 3,456,789

Practical No: 15 Develop a program that is likely to throw multiple exceptions that are handled using catch and finally block

using System; using System.Collections.Generic; using System.Text; namespace MutipleCatch { class MultipleCatchBlockExample { public void execute() { try { double val1=0; double val2=121; Console.WriteLine("Dividing {0} by {1}",val1,val2); Console.WriteLine("{0}/{1}={2}",val1,val2,DivideValues(val1,val2)); } //most specific exception type first catch(DivideByZeroException ex) { Console.WriteLine("DivideByZeroException caught!",ex); }

catch(ArithmeticException e) { Console.WriteLine("ArithmeticException caught!",e); } //generic exception type last catch { Console.WriteLine("Unknown Exception caught"); } finally { Console.WriteLine("Exceptions are caught"); } } //do the division if legal public double DivideValues(double val1,double val2) { if(val2==0) { DivideByZeroException dx=new DivideByZeroException(); Console.WriteLine(dx.Message); } if(val1==0) { ArithmeticException ax=new ArithmeticException();

Console.WriteLine(ax.Message); } return val1/val2; } static void Main(string[] args) { Console.WriteLine("This is an example of multiple catch and finally blocks and each catch block handle errors/exceptions of a certain type thrown in the application.\n"); MultipleCatchBlockExample mcbobj=new MultipleCatchBlockExample(); mcbobj.execute(); Console.ReadLine(); } } } OUTPUT: This is an example of multiple catch and finally blocks and each catch block handle errors/exceptions of a certain type thrown in the application. Dividing 0 by 121 Overflow or underflow in the arithmetic operation. 0/121=0 Exceptions are caught

You might also like