Lab - 1 Solution : 1 // Building a Simple Console Application class HelloCsharp static void Main() System.Console.WriteLine ("Hello from C#."); Solution: 2 & 3 // Building a WPF Application // Verifying the Application Open Visual Studio 2008 and choose "File", "New", "Project..." in the main menu. Choose "WPF Application" as project type. Choose a folder for your project and give it a name. Then press "OK"
Visual Studio creates the project and automatically adds some files to the solution. A Window1.xaml and an App.xaml. The structure looks quite similar to WinForms, except that the Window1.designer.cs file is no longer code but it's now declared in XAML as Window1.xaml/MainWindow.xaml Open the Window1.xaml/MainWindow.xaml file in the WPF designer and drag a Button and a TextBox from the toolbox to the Window Select the Button and switch to the event view in the properties window (click on the little yellow lightning icon). Doubleclick on the "Click" event to create a method in the codebehind that is called, when the user clicks on the button.
Visual Studio automatically creates a method in the code-behind file that gets called when the button is clicked. private void button1_click(object sender, RoutedEventArgs e) textbox1.text = "Hello WPF"; The textbox has automatically become assigned the name textbox1 by the WPF designer. Set text Text to "Hello WPF!" when the button gets clicked and we are done! Start the application by hit [F5] on your keyboard.
Solution : 4 // Generating Documentation for an Application namespace Hello /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window public MainWindow() InitializeComponent(); private void button1_click(object sender, RoutedEventArgs e) textbox1.text = "Hello WPF";
Solution: 1 Lab - 2 // Calculating Square Roots with Improved Accuracy public static int Isqrt(int num) if (0 == num) return 0; // Avoid zero divide int n = (num / 2) + 1; // Initial estimate, never low int n1 = (n + (num / n)) / 2; while (n1 < n) n = n1; n1 = (n + (num / n)) / 2; // end while return n; // end Isqrt() Solution: 2 // Converting Integer Numeric Data to Binary namespace ConsoleApplication1 class Program static void Main(string[] args) Console.Write("Number: "); int datanumber = int.parse(console.readline()); int remainder; string result = string.empty;
while (datanumber > 0) remainder = datanumber % 2; datanumber /= 2; result = remainder.tostring() + result; Console.WriteLine("Binary: 0", result); Console.ReadKey();
Lab - 3 Solution: 1 //Calculating the Greatest Common Divisor of Two Integers by Using Euclid's Algorithm namespace ConsoleApplication1 class Program static void Main(string[] args) Console.WriteLine("GCD of 0 and 1 is 2", 1, 1, gcd(1, 1)); Console.WriteLine("GCD of 0 and 1 is 2", 1, 10, gcd(1, 10)); Console.WriteLine("GCD of 0 and 1 is 2", 10, 100, gcd(10, 100)); Console.WriteLine("GCD of 0 and 1 is 2", 5, 50, gcd(5, 50)); Console.WriteLine("GCD of 0 and 1 is 2", 8, 24, gcd(8, 24)); Console.WriteLine("GCD of 0 and 1 is 2", 36, 17, gcd(36, 17)); Console.WriteLine("GCD of 0 and 1 is 2", 36, 18, gcd(36, 18)); Console.WriteLine("GCD of 0 and 1 is 2", 36, 19, gcd(36, 19)); for (int x = 1; x < 36; x++) Console.WriteLine("GCD of 0 and 1 is 2", 36, x, gcd(36, x)); Console.Read(); private static int gcd(int a, int b) int t;
// Ensure B > A if (a > b) t = b; b = a; a = t; // Find while (b!= 0) t = a % b; a = b; b = t; return a;
Solution: 2 // Calculating the GCD of Three, Four, or Five Integers namespace ConsoleApplication1 class Program static void Main() Console.WriteLine(GCD(new[] 10, 15, 30, 45 )); Console.ReadKey(); static int GCD(int a, int b) return b == 0? a : GCD(b, a % b); static int GCD(int[] integerset) return integerset.aggregate(gcd);
Lab - 4 Solution: 1 // Find the second largest element namespace ConsoleApplication1 class Program static void Main() int[] myarray = new int[] 0, 1, 2, 3, 13, 8, 5 ; int largest = int.minvalue; int second = int.minvalue; foreach (int i in myarray) if (i > largest) second = largest; largest = i; else if (i > second) second = i; System.Console.WriteLine(second); Console.ReadKey(); Solution: 2 // Multiplying Matrices namespace ConsoleApplication1 class Program static void Main(string[] args) int i, j;
int[,] a = new int[2, 2]; Console.WriteLine("Enter no for 2*2 matrix"); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) a[i, j] = int.parse(console.readline()); Console.WriteLine("First matrix is:"); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) Console.Write(a[i, j] + "\t"); Console.WriteLine(); int[,] b = new int[2, 2]; Console.WriteLine("Enter no for 2*2 matrix"); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) b[i, j] = int.parse(console.readline());
Console.WriteLine("second matrix is:"); for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) Console.Write(b[i, j] + "\t"); Console.WriteLine(); Console.WriteLine("Matrix multiplication is:"); int[,] c = new int[2, 2]; for (i = 0; i < 2; i++) for (j = 0; j < 2; j++) c[i, j] = 0; for (int k = 0; k < 2; k++) c[i, j] += a[i, k] * b[k, j]; for (i = 0; i < 2; i++) for (j = 0; j < 2; j++)
Console.Write(c[i, j] + "\t"); Console.WriteLine(); Console.ReadKey(); Solution: 3 //Find sum of all the elements present in a jagged array of 3 inner arrays namespace ConsoleApplication1 class Program int[][] jagged = new int[3][]; public void ReadArrays() Console.Write("\n Enter the size of First inner array: "); jagged[0] = new int[int.parse(console.readline())]; Console.WriteLine("\n Enter the elements of First inner array: "); for (int i = 0; i < jagged[0].length; i++) jagged[0][i] = int.parse(console.readline());
Console.Write("\n Enter the size of Second inner array: "); jagged[1] = new int[int.parse(console.readline())]; Console.WriteLine("\n Enter the elements of Second inner array: "); for (int i = 0; i < jagged[1].length; i++) jagged[1][i] = int.parse(console.readline()); Console.Write("\n Enter the size of Third inner array: "); jagged[2] = new int[int.parse(console.readline())]; Console.WriteLine("\n Enter the elements of Third inner array: "); for (int i = 0; i < jagged[2].length; i++) jagged[2][i] = int.parse(console.readline()); public void FindSum() int sum = 0; for (int i = 0; i < jagged[0].length; i++) sum = sum + jagged[0][i]; for (int i = 0; i < jagged[1].length; i++) sum = sum + jagged[1][i]; for (int i = 0; i < jagged[2].length; i++) sum = sum + jagged[2][i]; Console.WriteLine("\n\n\n Sum of all the three inner arrays is = 0", sum); public void PrintArrays() Console.Write("\n\n Elements of First inner array: "); for (int i = 0; i < jagged[0].length; i++)
Console.Write(jagged[0][i] + "\t"); Console.Write("\n\n Elements of Second inner array: "); for (int i = 0; i < jagged[1].length; i++) Console.Write(jagged[1][i] + "\t"); Console.Write("\n\n Elements of Third inner array: "); for (int i = 0; i < jagged[2].length; i++) Console.Write(jagged[2][i] + "\t"); class ArrayTest public static void Main() Program JA = new Program(); JA.ReadArrays(); JA.PrintArrays(); JA.FindSum(); Console.ReadLine();
Lab - 5 Q) Write a C# program with a class named Animal, make the animal class as Base class and Inheritance Animal class properties in Dog class and Cat class? Animal Class using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AnimalIh class Animal public void sleep() Console.WriteLine("Sleeping"); public void eating() Console.WriteLine("Eating");
Cat Class using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AnimalIh class Cat:Animal public void meow() Console.WriteLine("Meow Meowwwwwwwww"); Dog Class using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace AnimalIh class Dog:Animal public void bark() Console.WriteLine("Whof Whof Whof Whof"); InheritanceEx Class using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Animal class InheritanceEx public static void Main(string[] args) Dog d = new Dog(); d.sleep();
d.bark(); Cat c = new Cat(); c.eating(); c.meow(); Console.ReadKey(); Procedure: 1. Create a New C# Console Application Project and Name it as AnimalIh. 2. By default you will get a class Named Program.cs with the Namespace AnimalIh 3. Rename Program.cs to Animal.cs and Click Yes button. 4. Write the Animal Class code inside it. 5. Goto Solution Explorer do right click on AnimalIh a drop down menu appears. 6. Select Add option you will get a submenu form that select class and name it as Cat.cs 7. Write the Cat Class code inside it. 8. Goto Solution Explorer do right click on AnimalIh a drop down menu appears. 9. Select Add option you will get a submenu form that select class and name it as Dog.cs 10. Write the Dog Class code inside it. 11. Goto Solution Explorer do right click on AnimalIh a drop down menu appears. 12. Select Add option you will get a submenu form that select class and name it as InheritanceEx.cs 13. Write the InheritanceEx Class code inside it. Save the solution and Run Debug button to get the below code. OUTPUT:
Lab - 6 Q1) Write a C# program to Generate Random Number using C# Console Application? Constructor without Parameters: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace RandomNumberGeneration class RandomNG static void Main(string[] args) Random r = new Random(); // Constructor without parameters int a = r.next(1000); string result = Convert.ToString(a); MessageBox.Show(result); OUTPUT:
Q2) Write a C# program to Generate Day, using C# Console Application? Constructor with Parameters: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace DateTimeEx class DateAndTime static void Main(string[] args) DateTime dt = new DateTime(2013, 11, 21);// Constructor with parameters int iday = dt.day; int imonth = dt.month; int iyear = dt.year; Console.WriteLine("MID-2 Exam Date:0-1-2",iday,imonth,iyear); // To Get Current Day Month and Year string Day = DateTime.Now.ToString("dd"); string Month = DateTime.Now.ToString("mm"); string Year = DateTime.Now.ToString("yyyy"); MessageBox.Show("Todays Date:"+Day+"-"+ Month+"-"+ Year); OUTPUT:
Lab - 7 Q1) Write a C# Program to Demonstrate Method Overloading? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace PolymorphismEx class OverloadingEx public void Add(int x, int y) int c = x + y; string result = Convert.ToString(c); MessageBox.Show("Addition of Numbers - "+result); public void Add(String a, String b) String c = a + b; MessageBox.Show("Addition of Strings - " + c); static void Main(string[] args) OverloadingEx ol = new OverloadingEx(); ol.add(20, 30); ol.add("object", "Oriented"); OUTPUT:
Q2) Write a C# Program to Demonstrate Method Overriding? OverridingM Class using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace OverridingEx class OverridingM public virtual void cal(int a, int b) MessageBox.Show("Calculation is started"); Calculation Class using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OverridingEx class Calculation:OverridingM public override void cal(int a, int b) int c = a + b; Console.WriteLine("Overriding method Add"); base.cal(a,b); Console.WriteLine(c); Console.ReadKey();
OverridingMain Class using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OverridingEx class OverridingMain public static void Main(string[] args) Calculation c = new Calculation(); c.cal(10, 20); OUTPUT: