You are on page 1of 44

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:

Khan

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

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

Khan Output:

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

Khan

Output:

Khan

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

Khan

Output:

10

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

11

Khan

Output:

12

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

13

Khan

Output:

14

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

15

Khan

Output:

16

Khan /*8. 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.*/ using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication10 { class Demo { public static void Main(string[] args) { string s = "Check"; string t = "Check"; if (s.Equals(t)) Console.WriteLine("Strings are Same"); else Console.WriteLine("String are Different"); Console.WriteLine("Length of the string s is= " + s.Length); Console.ReadLine(); } } }

17

Khan

Output:

18

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

19

Khan

Output:

20

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

21

Khan

Output:

22

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

23

Khan

Output:

24

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

25

Khan

Output:

26

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

27

Khan

Output:

28

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

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

30

Khan

Output:

31

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

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

33

Khan

Output:

34

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

Khan } Output:

36

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

37

Khan

Output:

38

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

39

Khan

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

Khan Console.Read(); } } } Output: Output for Blue:

Output for Green:

41

Khan

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

Khan else if (ob == Mode.on) Console.WriteLine("on"); else Console.WriteLine("invalid number"); Console.ReadLine(); } } }

Output: Output for ON Condition:

43

Khan

Output for OFF Condition:

44

You might also like