Khan

List of Programs:
1.

WAP to print “HELLO C#” 3 WAP to read two integer numbers and print the sum 6 WAP to input and output 5 numbers using array and function 8 WAP using 2D array to print a matrices 10 WAP to print jagged array and also find the number of rows and number of columns in each row 12 WAP to find sum of two number using function 14 WAP to show how static data member works in a class 16 WAP to check whether the two entered string are equal or not using string class and its methods. also find the length of each string 18 WAP to show how we can use structure in a class 20 to show how structure variable works differently than class variable (object) 22 to show how we can pass object as an argument to a function 24 that will work like copy constructor of c++
1

2.

3.

4.

5.

6.

7.

8.

9.

10. WAP

11. WAP

12. WAP

26

Khan
13. WAP

that contains different classes but no inheritance

28
14. WAP

to inherit base class into derived class (no method overriding) 30 to show polymorphism using virtual and override keyword 33 to show how we can use abstract class and abstract method 36 to show multiple inheritance using class and interface to avoid inheritance of particular method 38

15. WAP

16. WAP

17. WAP

18. WAP

40
19. WAP

to show how we can use enumeration to define symbolic constants for color 41 to show how we can use enumeration to define symbolic constants for on/off (1,0) values 43

20. WAP

// 1. WAP to print “HELLO C#” using System; using System.Collections.Generic; using System.Text; namespace printname 2

Khan { class Program { static void Main(string[] args) { Console.WriteLine("HELLO C#"); Console.ReadLine(); } } }

Output:

3

Khan

// 2. WAP to read two integer numbers and print the sum using System; 4

Collections.ToInt32(s1). string s1 = Console.ReadLine(). int s3 = a1 + a2.ToInt32(s2).Text.ReadLine(). Console. string s2 = Console.Khan using System. int a2 = Convert.WriteLine("Enter the second number:"). Console. Console.Generic. using System.WriteLine("The sum is:" + s3). int a1 = Convert. namespace ConsoleApplication3 { class Program { static void Main(string[] args) { Console.WriteLine("Enter the first number:"). } } } 5 .ReadLine().

Khan Output: 6 .

ReadLine()). Console. using System. for (int i = 0.Length. i < a.Text. i++) Console.Collections. Console. GetArray(a). namespace ConsoleApplication { class Array { static void Main(string[] args) { int[] a = new int[5].WriteLine("ENTERED NUMBER IS\n"). } } } 7 . i++) a[i] = int. for (int i = 0. i < a.Parse(Console.WriteLine("ENTER 5 NUMBERS").ReadLine().WriteLine("a[" + i + "]=" + a[i]). WAP to input and output 5 numbers using array and function using System.Generic.Khan // 3. } static void GetArray(int[] a) { Console. using System.Length.

Khan Output: 8 .

WriteLine(). a[1. a[1. using System. using System. 0] = 50. a[0. j < 3. 3].ReadLine(). 2] = 100. 0] = 80.Write(" " + a[i. Console. for (int i = 0. a = new int[2.Collections. } Console. namespace ConsoleApplication6 { class TwoDArray { static void Main(string[] args) { int[. a[0. WAP using 2D array to print a matrices. 1] = 90. 1] = 60. i++) { for (int j = 0.WriteLine("press any key to exit").Khan // 4. } } } 9 . a[0.Text. using System. j++) Console. 2] = 70. a[1.] a.Generic. i < 2. Console. j]).

Khan Output: 10 .

*/ using System. WAP to print jagged array and also find the number of rows and number of columns in each row.56}.Write(" " + a[i][j]). a[1] = new int[] {12.94. namespace ConsoleApplication7 { class Demo { static void Main(string[] args) { int[][] a = new int[2][]. Console. } Console.WriteLine("no of column in first row=" + a[0].Text. Console. } } } 11 . for (int i = 0.WriteLine("\npress any key to exit").Collections. Console. Console.46.Khan /* 5.ReadLine().WriteLine("no of rows=" +a.Length).Length. Console.Generic. a[0] = new int[] { 89.Length). i++) { for (int j = 0.Length).WriteLine().WriteLine("no of column in second row=" + a[1].j++) Console.54. i < a.64 }.Length. using System. j < a[i]. using System.

Khan Output: 12 .

Write("Enter 1st Number ").Parse(Console. using System. WAP to find sum of two number using function. Console.WriteLine("Sum of " + a + " & " + b + " is =" + c). return c.Text.Write("Enter 2nd Number "). } static int sum(int a. using System.ReadLine()).ReadLine()).Generic. namespace ConsoleApplication8 { class Sumof { static void Main(string[] args) { int a.Khan // 6. } } } 13 . int c = sum(a. b = int. b.Collections.Parse(Console.ReadLine(). int b) { int c = a + b. Console. b). using System. a = int. Console. Console.

Khan Output: 14 .

Get(99). A b = new A(). b. using System.Get(200). a. } public void Show() { Console.Khan // 7. using System. } } } 15 . namespace ConsoleApplication9 { class A { static int n. b. using System. } } class sta_tic { static void Main() { A a = new A().Text. a.Show(). public void Get(int x) { n = x.Generic.Show(). a.Show().Collections.WriteLine("n=" + n).ReadLine(). WAP to show how static data member works in a class. Console.

Khan Output: 16 .

using System.Collections.Text.Length).WriteLine("Strings are Same"). string t = "Check". } } } 17 . also find the length of each string.Equals(t)) Console.Khan /*8.*/ using System.WriteLine("String are Different"). WAP to check whether the two entered string are equal or not using string class and its methods.WriteLine("Length of the string s is= " + s. namespace ConsoleApplication10 { class Demo { public static void Main(string[] args) { string s = "Check".ReadLine(). Console. using System.Generic. Console. if (s. else Console.

Khan Output: 18 .

Collections. string name. public void SetData(int r. namespace ConsoleApplication11 { struct Student { int rollno. using System. using System. s. string n) { rollno = r. s.ReadLine(). Console.Text.Generic. "Tanmay Baid"). } public void ShowData() { Console.SetData(52.ShowData(). Console. } } } 19 .WriteLine("Name=:" + name). } } class struc { static void Main(string[] args) { Student s = new Student(). using System. name = n. WAP to show how we can use structure in a class.Khan // 9.WriteLine("ROLL NO=:" + rollno).

Khan Output: 20 .

WriteLine(rollno + " " + name). // values of s will be copied into t t.ShowData().ShowData(). using System.Text.Collections. s.ShowData(). "Atif"). s. s. } } } 21 . string n) { rollno = r. } } class strt { static void Main(string[] args) { Student s = new Student(). Student t = s. // s will not change t. string name. } public void ShowData() { Console. "Enrique").ShowData().SetData(2. namespace ConsoleApplication12 { struct Student { int rollno.SetData(1. using System. name = n. WAP to show how structure variable works differently than class variable (object). public void SetData(int r. using System. t.Generic.ReadLine().Khan // 10. Console.

Khan Output: 22 .

t. t. // s will not change t. Console.ReadLine(). t.ShowData().SetData(2.ShowData().name. using System. name = a. Student t = new Student(). namespace ConsoleApplication13 { class Student { int rollno. } public void ShowData() { Console. WAP to show how we can pass object as an argument to a function. name = n. } public void SetData(Student a) { rollno = a. "Robin"). using System.Khan // 11. s.Text.Collections. "Shirlee").rollno. s. public void SetData(int r.SetData(s).Generic. } } class Demo { static void Main(string[] args) { Student s = new Student().WriteLine(rollno + " " + name). string name.ShowData().ShowData().SetData(1. } } } 23 . using System. s. string n) { rollno = r.

Khan Output: 24 .

public void SetData(int r.Khan // 12.ReadLine(). s.ShowData().ShowData(). string n) { rollno = r.WriteLine(rollno + " " + name). t. s. Student t = s.SetData(1.// s will also be changed t.Generic. } } class Demo { static void Main(string[] args) { Student s = new Student(). name = n.ShowData(). using System. "Gabriel").// t will point to s t. using System. WAP that will work like copy constructor of c++.Collections.SetData(2. } } } 25 . Console. namespace ConsoleApplication14 { class Student { int rollno.Text. s.ShowData(). string name. } public void ShowData() { Console. "Rohinton"). using System.

Khan Output: 26 .

B b = new B().WriteLine("Show A").Khan // 13.ReadLine().Text. b. WAP that contains different classes but no inheritance. using System.Show().Generic. } } } 27 . Console. namespace ConsoleApplication15 { class A { public void Show() { Console.Show().Collections.WriteLine("Show B"). a. } } class Demo { static void Main(string[] args) { A a = new A(). } } class B { public void Show() { Console. using System. using System.

Khan Output: 28 .

WriteLine("Langston Hughes").Say(). b. WAP to inherit base class into derived class (no method overriding).Text.Generic. } } class B : A { public void Say() { Console.Call(). a.Collections.Say(). //a. } } class Demo { static void Main(string[] args) { A a = new A().WriteLine("*Octavia Butler"). namespace ConsoleApplication16 { class A { public void Say() { Console. using System. a. B b = new B().WriteLine("say B").WriteLine("Call B").WriteLine("write A").Say().WriteLine("--when (a=b)--").call().Write().Write(). a. Console. 29 .Khan // 14. using System. a = b. } public void Write() { Console.WriteLine("say A"). Console. // call() not accessible by A's abject Console. using System. b. } public void Call() { Console. b.

// we cant call call() bcozz it is not defined by A class Console.Write(). } } } 30 . // a.Khan a.Call().ReadLine().

Khan Output: 31 .

Collections.WriteLine("***X***"). // call() not accessible by A's abject B b = new B(). b. } public virtual void Fun() { Console. //a.WriteLine("Call B").Fun(). WAP to show polymorphism using virtual and override keyword . b.Show().WriteLine("Show A"). Console.Text. using System. } } class B : A { public override void Show() { Console.Khan // 15.Show(). Console. a = b.WriteLine("Show B"). } public void Call() { Console.WriteLine("***Y***"). using System. a. b.Generic. a. 32 .Call(). a. using System. namespace ConsoleApplication17 { class A { public virtual void Show() { Console.Fun(). } } class Demo { static void Main(string[] args) { A a = new A().WriteLine("***when(a=b)***").Show().Call(). Console.WriteLine("Fun A").

} } } 33 . // we cant call call() bcozz it is not defined by A class Console.ReadLine(). // a.Call().Khan a.Fun().

Khan Output: 34 .

Show().Fun().Text. Console.Call(). A a = b. using System.ReadLine(). b.Generic. Console.Fun().WriteLine("----B----"). using System. } } 35 . } class B : A { public override void Show() { Console. } public override void Call() { Console.WriteLine("Show A").WriteLine("Call B").Khan // 16. b.WriteLine("----(A=B)----").WriteLine("Fun A"). } } class Demo { static void Main(string[] args) { B b = new B(). WAP to show how we can use abstract class and abstract method.WriteLine("Show B"). } public abstract void Call(). using System.Collections. a. a.Call(). a. } public virtual void Fun() { Console. namespace ConsoleApplication18 { abstract class A { public virtual void Show() { Console. Console. b.Show().

Khan } Output: 36 .

} public void Call() { Console.Khan // 17. namespace ConsoleApplication19 { class A { public virtual void Show() { Console. b.Show().WriteLine("Show A").Call(). using System.Generic. } } class Demo { static void Main(string[] args) { B b = new B(). Console. Console.WriteLine("----B---").WriteLine("Show B"). } public void Disp() { } } interface Z { void Call(). // will call show() of B class b.WriteLine("Call B"). WAP to show multiple inheritance using class and interface.Show(). a. // will call call() of B class A a = b.Text.ReadLine(). } class B : A.Collections. using System. Z { public override void Show() { Console. using System. // will call show() of B class Console.WriteLine("---(A=B)--"). } } } 37 .

Khan Output: 38 .

namespace ConsoleApplication20 { class A { public virtual void Show() { Console. WAP to avoid inheritance of particular method. } } class B : A { public sealed override void Show() { Console. A a = b. } } class C : B { public override void Show() // Error :sealed method in B cant be override { Console.WriteLine("Show A").ReadLine().Collections. } } class Demo { static void Main(string[] args) { B b = new B(). using System.Show().WriteLine("Show B"). a.WriteLine("Show B").Generic. using System.Text. Console.Khan // 18. } } } 39 . using System.

WriteLine("thanks!!"). using System.WriteLine("\nYour choise of colour is. buffer = Console.WriteLine("Green=1")..Khan // 19. Console.WriteLine("Violet=0"). Console. myColor = (Color)Convert. switch (myColor) { case Color.Voilet: System.").WriteLine("\nYour choise of colour is:.WriteLine("Blue=2"). break.WriteLine("\nYour choise of colour is.Blue: System..Console. case Color.Green: System. (int)myColor). Console. using System.Text. using System. break. WAP to show how we can use enumeration to define symbolic constants for color. Console."). default: System. } static void Main(string[] args) { string buffer... Console. Color myColor. case Color.. break. 40 .").Collections.WriteLine("Enter a value for a color:")..Console. Blue..ReadLine().WriteLine("\nYour choise of colour is. namespace ConsoleApplication21 { class Colors { enum Color { Voilet.").Generic. myColor. } System.Console.WriteLine("\n {0} ({1})".Console. break. Green..ToInt32(buffer).Console.

} } } Output: Output for Blue: Output for Green: 41 .Read().Khan Console.

Collections.0) values.off) Console. WAP to show how we can use enumeration to define symbolic constants for on/off (1. 42 . namespace ConsoleApplication22 { class TTT { public enum Mode { off.*/ using System.Khan /* 20.Generic.Text. if (ob == Mode. Console. using System.WriteLine("enter 0 or 1").WriteLine("off").ToByte(str). using System. string str = Console. on } static void Main(string[] args) { Mode ob. ob = (Mode)Convert.ReadLine().

Console.WriteLine("invalid number").Khan else if (ob == Mode.WriteLine("on"). else Console. } } } Output: Output for ON Condition: 43 .on) Console.ReadLine().

Khan Output for OFF Condition: 44 .