CC

January 3, 2017 | Author: Raj Pattanayak | Category: N/A
Share Embed Donate


Short Description

Download CC...

Description

C# PROGRAMS At a Glance

1. /* BASE:- this is used to pass the value from derived class constructor to the base class constructor */ namespace base_kw_usage { class A { public int a; public int b; public A() { } public A(int a, int b) { this.a = a; this.b = b; } public void sub() { int s = a - b; Console.WriteLine("subtraction of two numbers {0} and {1} results {2}",a,b,s); } } class B:A { public int a; public int b; public int c; public B(int a, int b, int c) : base(a, b) // By using this base method the value of a and b from this contructor is transmitted o the constructor of the base class {

this.a = a; this.b = b; this.c = c;

} public int add() { int z = a + b + c; return z; } } class Program { static void Main(string[] args) { B b = new B(15,6,4); // Constructor initialized at the object creation step int x=b.add(); Console.WriteLine("sum is "+x); b.sub(); // the value from the derived class constr. will get passed to base class constr. with that base method usage } } }

2. namespace Findlargest { class Program { static void Main(string[] args) { Console.WriteLine("Enter any ten elements to compare for largest number");

large();

} public static void large() { int[] input ={ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; for (int i = 0; i < 9; i++) { int a = Convert.ToInt32 (Console.ReadLine()); input[i] = a; } for (int k = 1; k input[k]) { } else { // int temp; // temp = input[0]; input[0] = input[k]; // input[k] = temp; } } Console.WriteLine("Largest no. is" + input[0]); } } }

3. class outkw { public int add(int a, int b,out int o) // method having argument and return value with out keyword in input argument { o = a + b; return o; } } namespace outkeywordus {

class Program { static void Main(string[] args) { int m; // variable declared for out variable outkw ok = new outkw(); int z=ok.add(30,7,out m); // variable m declared with keyword out to get from the function Console.WriteLine(z); } } }

4. class Program { static void Main(string[] args) { int[] list ={ 1,2,3,4,5,6,7,8,9}; Console.WriteLine("Even numbers"); for (int i = 0; i < 9; i++) { if (list[i]%2 == 0) { Console.WriteLine(list[i]); } } Console.WriteLine("Odd numbers"); for (int i = 0; i < 9; i++) { if (list[i] %2!= 0) { Console.WriteLine(list[i]); } } }

}

5. class MatrixA { public int[,] mata = new int[3, 3]; //Declaring the matrix A to have 3 rows and 3 columns public int i; public int j; public void matrixa() { Console.WriteLine("Enter the Matrix A elements\n"); { for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++)

{

mata[i, j] = Convert.ToInt32(Console.ReadLine());

} }

}

}

} class MatrixB : MatrixA // Interfacing Matrix A to Matrix B { public int[,] matb = new int[3, 3]; //Declaring the matrix B to have 3 rows and 3 columns public int i; public int j; public void matrixb() { matrixa(); Console.WriteLine("Enter the Matrix B elements\n"); {

}

for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { matb[i, j] = Convert.ToInt32(Console.ReadLine()); } }

}

} class MatrixAdd : MatrixB // Interfacing the matrix B to this class { public int[,] matc = new int[3, 3];//Declaring the matrix C to have 3 rows and 3 columns public int j; public int i; public void matadd() { matrixb(); Console.WriteLine("Matrix A is\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write("\t" + mata[i, j]); } Console.WriteLine(); Console.WriteLine(); } Console.WriteLine("Matrix B is\n"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write("\t" + matb[i, j]); } Console.WriteLine(); Console.WriteLine(); } Console.WriteLine("Matrix C is\n"); for (i = 0; i < 3; i++)

{

} }

for (j = 0; j < 3; j++) { matc[i, j] = mata[i, j] + matb[i, j]; Console.Write("\t" + matc[i, j]); } Console.WriteLine(); Console.WriteLine();

}

class Program { static void Main(string[] args) { MatrixAdd ma = new MatrixAdd(); // mm.matrixa();accessing the method by object // mm.matrixb(); ma.matadd();// accessing the parent classes through the derived class object by having the methods included in the class by interface } }

6. class MatrixA { public int[,] mata = new int[3, 3]; public int i;public int j; public void matrixa() { Console.WriteLine("Enter the Matrix A elements\n"); { for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { mata[i,j] = Convert.ToInt32(Console.ReadLine()); } }

}

}

} class MatrixB:MatrixA { public int[,] matb = new int[3, 3]; public int i; public int j; public void matrixb() { Console.WriteLine("Enter the Matrix B elements\n"); {

for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) {

} }

matb[i, j] = Convert.ToInt32(Console.ReadLine());

}

} } class MatrixMult:MatrixB // Interfacing the matrix A to this class { public int[,] matc = new int[3, 3]; public int j; public int i; public void matmul() { Console.WriteLine("Matrix A is"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write("\t"+mata[i, j]); } Console.WriteLine(); Console.WriteLine(); } Console.WriteLine("Matrix B is"); for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { Console.Write("\t"+matb[i, j]); } Console.WriteLine(); Console.WriteLine(); } Console.WriteLine("Matrix C is"); for (i = 0; i < 3; i++) { for ( j = 0; j < 3; j++) {

} }

matc[i,j]=mata[i,j]*matb[i,j]; Console.Write("\t"+matc[i, j]); } Console.WriteLine(); Console.WriteLine();

}

class Program { static void Main(string[] args) { MatrixMult mm = new MatrixMult(); mm.matrixa(); mm.matrixb(); mm.matmul(); } }

7. class paramex {

public static void parray(params int[] m) { Console .WriteLine("array elements"); foreach (int i in m) { Console.WriteLine(i); } }

} namespace paramsusage { class Program { static void Main(string[] args) { int[] x ={ 1,2,3}; paramex. parray(x); paramex.parray(); paramex.parray(100,200); } } }

8. class ClassI { public int x; public int y; public ClassI() { Console.WriteLine(@" ClassI: Default Constructor is employed in this class ClassII: addition process done and the result is "); } } class ClassIII:ClassI { public ClassIII() { Console.WriteLine("Class III accessed"); } } class ClassII : ClassIII { public ClassII(int a, int b) { this.x = a; this.y = b; } public void adder() { int c = x + y; Console.WriteLine(c); } } namespace Inheritanceex { class Program { static void Main(string[] args) { ClassII c2 = new ClassII(5,5); c2.adder(); } }

}

9. SUM OF DIGITS class SoDprocess { public void SoDcal(int a) { int b; int sum = 0; while (a != 0) { b = a % 10; a = a / 10; sum = sum + b; } Console.WriteLine("The sum of digits in given number is } } namespace sumofdigitsclass { class Program { static void Main(string[] args) { int m; Console.WriteLine("Enter a number"); string ah = Console.ReadLine(); m = Int32.Parse(ah); SoDprocess sod = new SoDprocess(); sod.SoDcal(m); } } }

10. namespace print_no_in_rev_order { class Program { static void Main(string[] args) { int[] input ={1,2,3,4,5,6,7,8,9,0}; { for (int i = 0; i < 10; i++) { input[i]=Int16 .Parse ( Console.ReadLine()); } // Array.Sort(input);// ascending sort Array.Reverse(input); foreach (int j in input) { Console .WriteLine (j); } }

" + sum);

}

}

}

11. class Input { public int x; public int y; public Input() { } /* public Input(int a, int b) { this.x = a; this.y = b; }*/ public void printer() { for (int i = 1; i amt) Console.WriteLine("Not sufficient balance you have only Rs: {0} in your account", amt); else { int b = amt - w; Console.WriteLine("Balance after your last transaction is" + b); } } public void deposit() // method for deposition { Console.WriteLine("Enter the amount to deposit"); int d = Int16.Parse(Console .ReadLine ()); int b = d + amt; Console.WriteLine("Your balance after your deposition of Rs {0} is {1}",d,b); } } // Start of Main Program class Program { static void Main(string[] args) { AccMgmt am = new AccMgmt(10000); // passing the value to the constructor by object Console.WriteLine("\t\t\tWelcome to My Bank\n\n"); start: Console.WriteLine("Select your choice"); Console.WriteLine(@" 1.Balance Enquiry 2.Cash Withdrawl 3.Deposition 4.Exit"); int i=Int16.Parse(Console.ReadLine ()); switch (i) {

case 1: am.benq(); Console.WriteLine("Would you like to have an other transaction. If yes press Y or press any key to exit"); string s=Console.ReadLine(); int n = string.Compare(s,"Y"); int nn = string.Compare(s, "y"); if(n==0 || nn==0) goto start; else break; case 2: Console.WriteLine("Enter the amount to withdraw "); int w = Int16.Parse(Console .ReadLine ()); am.withdrawl(w); Console.WriteLine("Would you like to have an other transaction. If yes press Y or press any key to exit"); string s1 = Console.ReadLine(); int n1 = string.Compare(s1, "Y"); int n11 = string.Compare(s1, "y"); if (n1 == 0 || n11==0) goto start; else break; case 3: am.deposit(); Console.WriteLine("Would you like to have an other transaction. If yes press Y or press any key to exit"); string s2 = Console.ReadLine(); int n2 = string.Compare(s2, "Y"); int n21 = string.Compare(s2, "y"); if (n2 == 0 || n21==0) goto start; else break; case 4: break; default: Console .WriteLine ("Sorry wrong option select the correct choice"); goto start; } } }

18. FACTORIAL USING CLASS class factorial { public void fact(int a) { int b = a; int c = a; { for (int i = 1; i < c; i++) { b = b - 1;

}

a = a * b; //a=a*(b-1); //b--;

} Console.WriteLine("Factorial for:" + b + "is" + a); }

} namespace factex { class MAinPgm { public static void Main(string [] args) { factorial fact=new factorial (); Console.WriteLine("Ehter a value to find the factorial"); string it=Console .ReadLine (); int i = Int32.Parse(it); Console.WriteLine(); fact.fact(i); } } }

19. SUM OF SQUARE OF DIGITS class squaredigit { int x; public squaredigit(int a) { this. x = a; } public void process() { int ss = 0; while (x != 0) { int y = x % 10; x = x / 10; ss = ss + (y*y); } Console.WriteLine(ss); } } namespace squareofdigit { class MainPgm { public static void Main(string[] args) { squaredigit sd = new squaredigit(55); sd.process(); } } }

20. SCOPE AND VISIBILTY OF PRIVATE AND PUBLIC MEMBERS // Showing the scope of private and public members in class namespace scope_of_pub_pri_mem { class A { int a; int b; int c; // private members declares and hence the scope of the members are within this class A only and the method using this private members cannot be inherited nor accessed by the object of the derived class public A(int x, int y) { this.a = x; this.b = y; } public int add() { return c = a + b; } } class B { public void sub( int x, int y) { //add(); //The method cannot be called here since the members involved in this are private int c; //return c = x - y; c = x - y; Console.WriteLine("Subtraction result : "+c); } } class C:B // class B is inherited in to this class { public int a; public int b; public int c; public void mul(int a,int b) { sub(a, b); // the members a and b since declared as public it is passed to the method sub() which is in class B c=a*b; Console .WriteLine ("Multiplication result: "+c); } } class Program { static void Main(string[] args) { A a = new A(5,5); int m=a.add(); Console.WriteLine("Addition result : " + m); B b = new B(); // b.sub();// cannot be accessed by object b of class B since it takes the input arguments from the class C // int z=b.sub(45,5);

// Console.WriteLine("Subtraction result : "+z); // b.add(); //cannot be accessed by the object b since it cannot pass any arguments to the private members of the class C c=new C (); c.mul(15,7); } } }

20. SWAPPING OF TWO NUMBERS WITHOUT USING A TEMPORARY VARIABLE namespace swapof2numwithouttempvaar { class Program { static void Main(string[] args) { int a; int b; a = Convert.ToInt16(Console.ReadLine()); b = Int16.Parse(Console.ReadLine() ); Console.WriteLine("Before swapping a={0} and b={1}", a, b); a = a + b; b = a-b; a = a-b; Console.WriteLine("After swapping a={0} and b={1}", a, b); }

}

}

21. SIMPLE ARRAYLIST EXAMPLE using System; using System.Collections;// Remove the generic from it to make arraylist to work using System.Text; namespace ArrayListex {

}

class Program { static void Main(string[] args) { ArrayList a = new ArrayList();// Declaring the arraylist a.Add("a"); // Adding elements to arraylist a.Add("b"); a.Add("c"); foreach (string s in a) { Console.WriteLine(s); } } }

22. ARRAYLIST EXAMPLE USING CLASS using System; using System.Collections;// Remove the generic from it to make arraylist to work using System.Text; // ArrayList class usge: Accessed by the object of the main class namespace ArrayListex2 { class ArrayListExample // Class defined for declaring the array list { public void getdata() { ArrayList al = new ArrayList(); //ArrayList declaration al.Add("venkatesh"); // Adding elements to arraylist al.Add(1); // Adding elements to arraylist al.Add("name"); // Adding elements to arraylist foreach (object i in al) { Console.WriteLine(i); } } } class Program { static void Main(string[] args) { ArrayListExample ale = new ArrayListExample(); ale.getdata(); } } }

23. STRING SORTING USING ARRAY LIST class Program { static void Main(string[] args) { ArrayList name = new ArrayList();// Array list declaration name.Add("Venkatesh"); // Adding elements starting from the index 0 name.Add("Babu"); name.Add("Kumar"); name.Add("Ramesh"); Console .WriteLine ("\nBefore Sorting\n"); foreach (string j in name) { Console.WriteLine(j); } Console.WriteLine("\nAfter sorting\n"); name.Sort(); foreach (string i in name) { Console.WriteLine(i); }

}

}

24. STRING SORTING USING ARRAY class Program { static void Main(string[] args) { string[] name ={ "venkat","kumar","zebastin","karna"}; Console.WriteLine("Before Sorting "); foreach (string j in name) { Console.WriteLine(j); } Console.WriteLine("After Sorting "); Array.Sort(name); foreach (string i in name) { Console.WriteLine(i); } } }

25. // PROGRAM TO SHOW HOW TO ACCESS THE MEMBER WHICH IS DECLARED AS A STATIC MEMBER class StatMemAcc { public static void disp() { Console.WriteLine("Static member is accessed"); } } namespace staticmemberaccess { class Program { static void Main(string[] args) { StatMemAcc.disp(); } } }

26. class stringarray { public static void Main() { string[] name=new string[] {"Mon", "Tue", "Wed"}; {foreach(object i in name) {Console.WriteLine(i);} } }

}

27. // // // // //

STRUCTURE is a value type It cannot be inherited STRUCTURE can have constructor and methods as like CLASS. It does not support default constructor It does not support INHERITANCE

namespace Structure2 { public struct baseclass { public int c; public int x; public int y; // public baseclass() // default constructor not possible in structures //{ } public baseclass(int a, int b) { // this.x = a; 'this' keyword is required for assigning for the variable which are alike // this.y = b; // this.c = a + b; x = a; y = b; c = a + b; } public void disp(baseclass b) { baseclass m; m.c = x+b.c; m.x = x + b.x; m.y = y + b.y; Console.WriteLine("c={0}\nx={1}\ny={2}",m.c,m.x,m.y); } } class MainProgram { static void Main(string[] args) { baseclass b = new baseclass(5, 4);// structure b.disp(b); Console.WriteLine(b.c); } } }

28. namespace structex3

initialization

{

struct details {public string name; public string adr; public int phn;public int pin; public string cty; public details(string n, string adr, int phn, int pin, string cty) { this.name = n; this.adr = adr; this.phn = phn; this.pin = pin; this.cty = cty; } public void getdet(details d) { // Console.WriteLine(" Name: {0}\nAddr: {1}\n City: {2}\n Pin no. : {3}\n Phone no. :{4}\n",d.name ,d.adr,d.cty ,d.pin ,d.phn ); Console.WriteLine(" Name: {0}\nAddr: {1}\n City: {2}\n Pin no. : {3}\n Phone no. :{4}\n", name, adr, cty, pin, phn); } } class Program { static void Main(string[] args) { Console.WriteLine("Enter your name"); string a = Console.ReadLine(); Console.WriteLine("Address"); string b = Console.ReadLine(); Console .WriteLine ("city"); string c = Console.ReadLine(); Console.WriteLine("Pin no"); int d = Int32.Parse(Console .ReadLine ()); Console.WriteLine("phone no."); int e = Int32.Parse(Console .ReadLine ()); details dt =new details (a,b,e,d,c); dt.getdet(dt); } } } Output: Enter your name Venkatesh Address geetha nagar city trichy Pin no 620017 phone no. 0 Name: Venkatesh Addr: geetha nagar City: trichy Pin no. :620017 Phone no. :0

29. namespace Structure_example { struct Example { public int x; public int y; /* public Example()  Structures cannot have a default constructor { }*/ public Example(int a, int b) // But structures can have the constructor with arguments { this.x = a; this.y = b; } public Example structaccess(Example e) // Structure employed as a datatype for both the input and output arguments { Example ex; // object created for the structure ex.x = x + e.x; // Accessing the variable by object ex.y = y + e.y; return ex; // returning the object } } class Program { static void Main(string[] args) { Example el = new Example(5, 5); // invoking the constructor class with input Example v; v=el.structaccess(el); Console.WriteLine(v.x + v.y); } } }

30. class GetArrayList {//public string list; public void getdata() { ArrayList list = new ArrayList(); list.Add("Index 0: Item1"); list.Add("Index 1: Item2"); list.Add("Index 2: Item3"); list.Add("Index 3: Item4"); disp(list); Console.WriteLine("Count" + list.Count); Console.WriteLine("Removing the item ftom the index 3"); list.RemoveAt(3); disp(list); Console.WriteLine("Count"+list .Count ); Console.WriteLine("Inserting a new item at index 2"); list.Insert(2, "new item inserted here"); disp(list);

Console.WriteLine("Count" + list.Count); Console.WriteLine("Printing the array list in reverse order"); list.Reverse(); disp(list); Console.WriteLine("Count" + list.Count); Console.WriteLine("Sorting the array list"); list.Sort(); disp(list); Console.WriteLine("Removing the index by range from 0 to 2"); list.RemoveRange(0, 2); Console.WriteLine("Clearing all contents from array list"); Console.WriteLine("All contents are cleared successfully"); list.Clear(); disp(list); } /*

public void cleararraylist() {

getdata();

}*/ public static void disp(ArrayList list) { foreach (object i in list) { Console.WriteLine(i); } }

} class Program { static void Main(string[] args) { GetArrayList g = new GetArrayList(); g.getdata(); } }

31. class sortdescend { public void DscendingSort(params int[] x) {for(int i=0;i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF