Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

C# First Assignment Q no 1: WAP to print size of all value types.

Solution:
Using System; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Console.WriteLine("================================================"); Console.WriteLine(" SIZE OF ALL VALUE TYPE"); Console.WriteLine("================================================\n"); Console.WriteLine("\tSize Of sbyte \t:: {0} Byte", sizeof(sbyte)); Console.WriteLine("\tSize Of byte \t:: {0} Byte", sizeof(byte)); Console.WriteLine("\tSize Of short \t:: {0} Bytes \n", sizeof(short)); Console.WriteLine("\tSize Of ushort \t:: {0} Bytes", sizeof(ushort)); Console.WriteLine("\tSize Of int \t:: {0} Bytes", sizeof(int)); Console.WriteLine("\tSize Of uint \t:: {0} Bytes", sizeof(uint)); Console.WriteLine("\tSize Of long \t:: {0} Bytes \n", sizeof(long)); Console.WriteLine("\tSize Of ulong \t:: {0} Bytes", sizeof(ulong)); Console.WriteLine("\tSize Of char \t:: {0} Bytes", sizeof(char)); Console.WriteLine("\tSize Of float \t:: {0} Bytes\n", sizeof(float)); Console.WriteLine("\tSize Of double \t:: {0} Bytes", sizeof(double)); Console.WriteLine("\tSize Of decimal\t:: {0} Bytes", sizeof(decimal)); Console.WriteLine("\tSize Of decimal\t:: {0} Bytes", sizeof(bool)); Console.WriteLine("================================================"); Console.ReadLine(); } } } ::OUTPUT:: ================================================ SIZE OF ALL VALUE TYPE ================================================ Size Of sbyte Size Of byte Size Of short Size Size Size Size Of Of Of Of ushort int uint long :: 1 Byte :: 1 Byte :: 2 Bytes :: :: :: :: 2 4 4 8 Bytes Bytes Bytes Bytes

Arya n |1

Size Of ulong Size Of char Size Of float

:: 8 Bytes :: 2 Bytes :: 4 Bytes

Size Of double :: 8 Bytes Size Of decimal :: 16 Bytes Size Of decimal :: 1 Bytes

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

Arya n |2

Q.2 Create a structure Name with member variables firstname, middlename and lastname. The structure contains a parameterized constructor to initialize the name. Create a class Employee with member variables: 1) struct variable of type Name 2) Designation of string type 3) Salary of decimal type Solution:
using System; namespace ConsoleApplication2 { public struct Name { public string first; public string middle; public string last; public Name(string f, string m, string l) { first = f; middle = m; last = l; } } } using System; namespace ConsoleApplication2 { class Emp { string s1,s2, s3 , des; double salary; Name n1; public void read() { Console.WriteLine("first name : \t"); s1 = Console.ReadLine(); Console.WriteLine("\nmiddle name : \t"); s2 = Console.ReadLine(); Console.WriteLine(" \nlast name :\t"); s3 = Console.ReadLine(); n1 = new Name(s1,s2,s3); Console.WriteLine("Designation : \t"); des = Console.ReadLine(); Console.WriteLine("Salary : \t"); salary = Convert.ToDouble(Console.ReadLine()); } public void write() { Console.WriteLine("Name : \t {0} {1} {2}" ,n1.first,n1.middle,n1.last); Console.WriteLine("Designation : \t{0}" , des); Console.WriteLine("Salary : \t{0}" , salary); } } }

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

Arya n |3

using System; namespace ConsoleApplication2 { class MyMain { static void Main(string[] args) { Emp employee = new Emp(); Console.WriteLine(" Employee\n \n"); employee.read(); Console.WriteLine("\n employee.write(); Console.ReadLine(); } } }

Enter the following detail about an

Employee Detail \n");

Output: Enter the following detail about an Employee

first name aryan middle name : last name : raghuvanshi Designation programmer Salary 40000.20

: :

Employee Detail Name : Designation Salary : : aryan raghuvanshi programmer 40000.2

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

Arya n |4

Q.3 Create a class called MyValue with a data member x of type Object. Now show the usage of the following methods in this class: 1) Equal() 2) GetType() 3) GetHashCode() 4) ToString() Solution:
using System; namespace ConsoleApplication1 { class MyValue { object variable; public MyValue( int variable ) { this.variable = variable; } } } using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { MyValue object1 = new MyValue(20); MyValue object2 = new MyValue(0); Console.WriteLine("\nobject 1 is equal to object 2 \t{0}", object1.Equals(object2)); Console.WriteLine("\nobject 1 is equal to object 1 \t{0}", object1.Equals(object1)); Console.WriteLine("\ntype of object 1 \t\t{0}", object1.GetType()); Console.WriteLine("\ntype of object 2 \t\t{0}", object1.GetType()); Console.WriteLine("\nhash code for object 1\t{0}", object1.GetHashCode()); Console.WriteLine("\nhash code for object 2\t{0}", object2.GetHashCode()); Console.WriteLine("\nconvert object 1 to string{0}", object2.ToString()); Console.ReadKey(); } } }

object 1 is equal to object 2 False object 1 is equal to object 1 True type of object 1 type of object 2 ConsoleApplication1.MyValue ConsoleApplication1.MyValue

hash code for object 1 37121646 hash code for object 2 45592480 convert object 1 to stringConsoleApplication1.MyValue

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment Q.4 WAP to explain the concept of Boxing and UnBoxing. Solution:
using System; namespace ConsoleApplication2 { public static class MyMain { public static void Main(String[] args) { int boxing; boxing = 10; Object ob = boxing; Console.Write("value of (boxing) ob : \t{0} \n" , ob); int unboxing = (int)ob; Console.Write("value of unboxing : \t{0}" , unboxing); Console.ReadLine(); } } } Output: value of (boxing) ob : value of unboxing : 10 10

Arya n |5

Q.5 WAP that reads a 1-D array of integers. Using binary search, find whether a number x is found in the array or not. Input x from user.
Solution: using System; namespace ConsoleApplication2 { class BinarySearch { public int[] array = new int[10]; public int i, j, n, temp; public int low, mid, high; public void read() { Console.WriteLine("Enter array size"); n=Convert.ToInt16( Console.ReadLine()); Console.WriteLine("Enter the elements of array\n"); for (i = 0; i < n; i++) { array[i] = Convert.ToInt16( Console.ReadLine()); } } //for sorted an array public void sort() { for (i = 0; i < n; i++) { for (j = 0; j < (n - i - 1); j++) { if (array[j] > array[j + 1]) { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } //end of sorted array public void arrayprint() {

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

Arya n |6

Console.WriteLine("Shorted Array Elements ::"); for (i = 0; i < n; i++) { Console.WriteLine("{0}", array[i]); } } public void search(int num) { low = 1; high = n; do { mid = (low + high) / 2; if (num < array[mid]) high = mid - 1; else if (num > array[mid]) low = mid + 1; } while (num != array[mid] && low <= high); if (num == array[mid]) { Console.WriteLine("\n\t{0} is present at array location {1}",array[mid], mid + 1); } else { Console.WriteLine("Search is FAILED\n"); } } //end of search } } using System; namespace ConsoleApplication2 { class MyMain { static void Main(string[] args) { BinarySearch b = new BinarySearch(); b.read(); b.sort(); b.arrayprint(); Console.WriteLine("enter a no for search:"); int num = Convert.ToInt16(Console.ReadLine()); b.search(num); Console.ReadLine(); } } }

Output:
Enter array size 4 Enter the elements of array 12 45 69 5 Shorted Array Elements :: 5 12 45 69 enter a no for search: 0 Search is FAILED

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment
Selection sort:
using System; namespace ConsoleApplication2 { public class SelectionSorting { int[] array = new int[10]; int i ,j , temp , n ; public void arraysize() { Console.WriteLine(" :: :: Enter the array Size n = Convert.ToInt16(Console.ReadLine()); } public void getArray() { Console.WriteLine(" :: :: Enter the value for an array for (i = 0; i < n; i++) { array[i] = Convert.ToInt16(Console.ReadLine()); } } //end of getArray public void selectSort() { for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(array[i]>array[j]) { temp=array[i]; array[i]=array[j]; array[j]=temp; } } } } //end of select Sort

Arya n |7

::

::

\n");

::

::

\n");

public void arrayDisplay() { Console.WriteLine("\n\n :: :: Sorted Array for (i = 0; i < n; i++) { Console.WriteLine("{0}", array[i]); } } } }

::

::

\n");

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

Arya n |8

using System; namespace ConsoleApplication2 { public static class MyMain { public static void Main(String[] args) { SelectionSorting obj = new SelectionSorting(); obj.arraysize(); obj.getArray(); obj.selectSort(); obj.arrayDisplay(); Console.ReadLine(); } } }

Output:

:: :: Enter the array Size :: :: 5 :: :: Enter the value for an array :: :: 32 1 5 4 4 :: :: Sorted Array :: :: 1 4 4 5

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

Arya n |9

Q.6 WAP that reads a 2-D jagged array of integers. Use linear search to find a given number in the array. Solution:
using System; namespace ConsoleApplication2 { class JaggedArray { int count = 0; public void jaggedarray() { Console.WriteLine("::: No fo rows for jagged array :::"); int r = Convert.ToInt16(Console.ReadLine()); int[][] j = new int[r][]; for (int i = 0; i < r; i++) { Console.WriteLine("Enter the size of ::::: {0} ::::: row", i); int c = Convert.ToInt16(Console.ReadLine()); j[i] = new int[c]; } Console.WriteLine("\n\n::::::::Enter the array value::::::"); for (int row = 0; row < j.Length; row++) { int[] aryan = j[row]; for (int col = 0; col < aryan.Length; col++) { aryan[col] = Convert.ToInt16(Console.ReadLine()); } } /*for (int row = 0; row < j.Length; row++) {int[] aryan = j[row]; Console.WriteLine("\t"); for (int col = 0; col < aryan.Length; col++) {Console.Write("{0} \t" , aryan[col]); // = Convert.ToInt16(Console.ReadLine()); } }* */ Console.WriteLine(" : : : Values entered by you foreach (var row in j) { Console.WriteLine(" \n "); foreach (var element in row) { Console.Write(" \t{0}", element); } } : : :");

Console.WriteLine("\n\n enter the Value you want to find"); int v = Convert.ToInt16(Console.ReadLine()); for (int row = 0; row < j.Length; row++) { int[] aryan = j[row]; for (int col = 0; col < aryan.Length; col++) { if (aryan[col] == v) {++count;

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment
Console.WriteLine("value found at {0} col); } } } *

A r y a n | 10
{1} location", row,

Console.WriteLine("variable found {0} Timesn in the array", count); } //end of jagged array } }

using System; namespace ConsoleApplication2 { public static class MyMain { public static void Main(String[] args) { JaggedArray m = new JaggedArray(); m.jaggedarray(); Console.ReadLine(); } } }

::: No fo rows for jagged array ::: 3 Enter the size of ::::: 0 ::::: row 2 Enter the size of ::::: 1 ::::: row 1 Enter the size of ::::: 2 ::::: row 3 ::::::::Enter the array value:::::: 4 6 2 1 6 4 : : : Values entered by you : : : 4 2 1 6 4 6

enter the Value you want to find 4 value found at 0 * 0 location value found at 2 * 2 location variable found 2 Times in the array

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment Q.7 WAP for sorting a 1-D array of strings, using: 1) Insertion sort 2)Selection sort Solution:
using System; namespace ConsoleApplication2 { public class InsertionSorting { int[] array = new int[10]; int i ,j , temp , n ; public void arraysize() { Console.WriteLine(" :: :: Enter the array Size n = Convert.ToInt16(Console.ReadLine()); } public void getArray() { Console.WriteLine(" :: :: Enter the value for an array for (i = 0; i < n; i++) { array[i] = Convert.ToInt16(Console.ReadLine()); } } //end of getArray public void insertSort() { for (i = 1; i < n; i++) { temp = array[i]; j = i - 1; while ( (temp < array[j]) && (j >= 0)) { array[j + 1] = array[j]; j = j - 1; } array[j + 1] = temp; } } //end of inser Sort public void arrayDisplay() { Console.WriteLine("\n\n :: :: Sorted Array for (i = 0; i < n; i++) { Console.WriteLine("{0}", array[i]); } } } }

A r y a n | 11

::

::

\n");

::

::

\n");

::

::

\n");

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment
using System; namespace ConsoleApplication2 { public static class MyMain { public static void Main(String[] args) { InsertionSorting obj = new InsertionSorting(); obj.arraysize(); obj.getArray(); obj.insertSort(); obj.arrayDisplay(); Console.ReadLine(); } } }

A r y a n | 12

:: :: Enter the array Size :: :: 5 :: :: Enter the value for an array :: :: 1 4 5 21 4 :: :: Sorted Array :: :: 1 4 4 5 21

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment Q.8 WAP to implement the Nested class concept. Solution:
using System; namespace ConsoleApplication2 { public class Outer { public void outerShow() { Console.WriteLine("hello i am in outer class"); } public class Inner { public void innerShow() { Console.WriteLine("hello i am in inner class"); } public static void Show() { Console.WriteLine("hello i am in inner static show "); } } //end of inner public Inner CreateObjectOfInnerClass() { Inner obj = new Inner(); return obj; } } }

A r y a n | 13

using System; namespace ConsoleApplication2 { public static class MyMain { public static void Main(String[] args) { Outer o = new Outer(); o.outerShow(); Console.ReadLine(); Outer.Inner i = new Outer.Inner(); i.innerShow(); Console.ReadLine(); Outer.Inner j = o.CreateObjectOfInnerClass(); j.innerShow(); Console.ReadLine(); Outer.Inner.Show(); Console.ReadLine(); } } }

hello i am in outer class hello i am in inner class hello i am in inner class hello i am in inner static show

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

A r y a n | 14

Q.9 WAP that defines a class Student. There are 2 data member name and rollno, with the respective properties associated with them. Also, there is an auto-implemented Boolean property status to indicate student is pass or fail Solution:
using System; namespace ConsoleApplication2 { public class Student { string name; int rollno; public string Name { get { return set { name = } public int Rollno { get { return set { rollno } public Boolean status { get;set;} public Student(int r , string n , Boolean b { Rollno = r; Name = n; status = b; } } }

name; value;

} }

rollno; = value; )

} }

using System; namespace ConsoleApplication2 { public static class MyMain { public static void Main(String[] args) { Student s = new Student(29, "aryan", true); Console.Write("Name \t{0} \n" , s.Name); Console.Write("Rollno \t{0} \n" , s.Rollno); if (s.status == true) { Console.WriteLine("\n\nResult: \t } else { Console.WriteLine("\n\nResult: \t } Console.ReadLine(); } }

PASS");

FAIL");

Name aryan Rollno 29 Result: PASS

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment Q.10 WAP to write three methods in a class. Use concept of multicasting in delegates, to invoke the method chain. Solution:
using System; namespace ConsoleApplication1 { delegate void StrMod(ref string str); class Program { static void ReplaceSpaces(ref string s) { Console.WriteLine("Replacing spaces with hyphens."); s = s.Replace(' ', '-'); } static void RemoveSpaces(ref string s) { string temp = ""; int i; Console.WriteLine("Removing spaces."); for (i = 0; i < s.Length; i++) if (s[i] != ' ') temp += s[i]; s = temp; } static void Reverse(ref string s) { string temp = ""; int i, j; Console.WriteLine("Reversing string."); for (j = 0, i = s.Length - 1; i >= 0; i--, j++) temp += s[i]; s = temp; } static void Main(string[] args) { StrMod strOp; StrMod replaceSp = ReplaceSpaces; StrMod removeSp = RemoveSpaces; StrMod reverseStr = Reverse; string str = "Hello I am Aryan"; strOp = replaceSp; strOp += reverseStr; strOp(ref str); Console.WriteLine("Resulting string: " + str); Console.WriteLine(); strOp -= replaceSp; strOp += removeSp; str = "Hello I am Aryan"; strOp(ref str); Console.WriteLine("Resulting string: " + str); Console.WriteLine(); Console.ReadKey(); } } }

A r y a n | 15

Output: Replacing spaces with hyphens. Reversing string. Resulting string: nayrA-ma-I-olleH Reversing string. Removing spaces. Resulting string: nayrAmaIolleH

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

A r y a n | 16

Q.11 WAP to define a class . The class has the following member variables: The class should contain: 1) Non parameterized constructor 2) Parameterized constructor 3) Static constructor 4) Indexer 5) Overload = = operator to compare 2 students Solution:
using System; namespace ConsoleApplication2 { class CompareStudent { public int[] marks = new int[10]; static string standard; public string name; public CompareStudent() { name = "aryan"; } public CompareStudent(string name) { this.name = name; } static CompareStudent() { standard = "mca"; } public static void dispstandard() { Console.WriteLine("{0}", standard); } public int this[int index] { get { return marks[index]; } set { marks[index] = value; } } public override string ToString() { return name; } public static bool operator ==(CompareStudent x, CompareStudent y) { return x.name == y.name; } public override bool Equals(object o) {

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment
return this == (CompareStudent)o; } public override int GetHashCode() { return 0; } public static bool operator !=(CompareStudent x, CompareStudent y) { return !(x == y); } } }

A r y a n | 17

using System; namespace ConsoleApplication2 { public static class MyMain { public static void Main() { CompareStudent c = new CompareStudent(); CompareStudent c1 = new CompareStudent("aryan"); c1[0] = 10; c[0] = 1; Console.WriteLine("Name of c object :{0}", c.ToString()); Console.WriteLine("Name of c1 object :{0}", c1.ToString()); Console.WriteLine("\nBoth name are equal :{0}", c == c1); Console.Write("\nStatic conststudent standard CompareStudent.dispstandard(); Console.Write("\nIndexer value of\n c1 Console.Write("\nBoth value are equal Console.ReadLine(); :");

: {0} \n c :{1} \t", c1[0] , c[0]); :{0}", c[0] == c1[0]);

} } }

Output: Name of c object Name of c1 object Both name are equal :aryan :aryan :True :mca

Static const student standard Indexer value of c1 : 10 c :1 Both value are equal

:False

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

A r y a n | 18

Q.12 WAP that implements a Fraction class that has two integral data members numerator and denominator. Define the following methods: 1) Method to calculate lcm 2) Method to calculate gcd 3) Conversion function from int to Fraction 4) Conversion function from Fraction to int 5) Overload operators + - * = = != < > 6) Overload ToString() method of Object class to display Fraction in the format num/denom. For eg Solution:
using System; namespace ConsoleApplication2 { class Fraction { int numerator; int denominator; public Fraction() { numerator = 0; denominator = 0; } public Fraction(int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; } public int lcm() { int n; for (n = 1; ; n++) { if (n % numerator == 0 && n % denominator == 0) return n; } } public int gcd() { int c; do { c = numerator % denominator; if (c == 0) return denominator; else { numerator = denominator; denominator = c; } } while (true); } public int toint(Fraction f) { numerator = (int) f.numerator; return numerator; }

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

A r y a n | 19

/*public Fraction tof() { fraction f = new fraction(); f = (Fraction)numerator;

return f; } * */ public static Fraction operator +(Fraction f, Fraction f2) { Fraction temp = new Fraction(); temp.numerator = f.numerator + f2.numerator; temp.denominator = f.denominator + f2.denominator; return temp; } public static Fraction operator -(Fraction f, Fraction f2) { Fraction temp = new Fraction(); temp.numerator = f.numerator - f2.numerator; temp.denominator = f.denominator - f2.denominator; return temp; } public static Fraction operator *(Fraction f, Fraction f2) { Fraction temp = new Fraction(); temp.numerator = f.numerator * f2.numerator; temp.denominator = f.denominator * f2.denominator; return temp; } public static bool operator ==(Fraction x, Fraction y) { return ((x.numerator == y.numerator)&&(x.denominator == y.denominator)); } public override bool Equals(object o) { return this == (Fraction)o; } public override int GetHashCode() { return 0; } public static bool operator !=(Fraction x, Fraction y) { return !((x.numerator == y.numerator) && (x.denominator == y.denominator)); }

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

A r y a n | 20

public static bool operator >(Fraction x, Fraction y) { return ((x.numerator > y.numerator) && (x.denominator > y.denominator)); }

public static bool operator <(Fraction x, Fraction y) { return ((x.numerator < y.numerator) && (x.denominator < y.denominator)); } public override string ToString() { string s = "\nnumerator " + numerator + " \n denominator " + denominator; return s; }

/* public static bool operator !=(Fraction x, Fraction y) { return !(x == y); } */ } } using System; namespace ConsoleApplication2 { public static class MyMain { public static void Main() { Console.Write("Enter the vaue numerator : "); int a = Convert.ToInt16(Console.ReadLine()); Console.Write("Enter the vaue denomenator: "); int b = Convert.ToInt16(Console.ReadLine()); Fraction f = new Fraction(a,b); int l = f.lcm(); int g = f.gcd(); Console.WriteLine("lcm ::{0}" ,l); Console.WriteLine("gcd ::{0}" ,g); int m = f.toint(f); Console.WriteLine("fraction to int

::{0}", m);

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

A r y a n | 21

Fraction f2 = new Fraction(a, b); Console.Write("Enter the vaue numerator for second obj : int c = Convert.ToInt16(Console.ReadLine()); Console.Write("Enter the vaue denomenator for second obj: int d = Convert.ToInt16(Console.ReadLine());

"); ");

Fraction f3 = new Fraction(c, d); Fraction pp = new Fraction(); pp = f2 + f3; Console.WriteLine(" + operator overload ::{0}", pp.ToString()); pp = f2 - f3; Console.WriteLine(" - operator overload ::{0}", pp.ToString()); pp = f2 *f3; Console.WriteLine(" * operator overload ::{0}", pp.ToString()); Console.WriteLine("both object comparable {0}", f3 == f2); Console.WriteLine("first obj is greater than second {0}", f2 > f3); Console.WriteLine("first obj is less than second {0}", f2<f3 ); Console.WriteLine("first obj is not equal to second {0}", f2 != f3); Console.ReadLine();

} } }

Output
Enter the vaue numerator : 20 Enter the vaue denomenator: 10 lcm ::20 gcd ::10 fraction to int ::20 Enter the vaue numerator for second obj : 22 Enter the vaue denomenator for second obj: 18 + operator overload :: numerator 42 denominator 28 - operator overload :: numerator -2 denominator -8 * operator overload :: numerator 440 denominator 180 both object comparable False first obj is greater than second False first obj is less than second True first obj is not equal to second True

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment Q.13 WAP to swap two integers in two ways: 1) Passing parameters by reference 2) Passing parameters by value
Solution: using System; namespace ConsoleApplication2 { class ByValueByRef { int a, b; public ByValueByRef() { a = 0; b = 0; } public ByValueByRef(int a, int b) { this.a = a; this.b = b; } public void callByValue(int a, int b) { int temp; temp = a; a = b; b = temp; Console.Write("\n\nIn Call By Value method } public void callByRef(ref int a, ref int b) { int temp; temp = a; a = b; b = temp; Console.Write("\n\nIn Call By Ref method } } }

A r y a n | 22

a:{0}\tb:{1}", a, b);

a:{0}\tb:{1}", a, b);

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

A r y a n | 23

using System; namespace ConsoleApplication2 { public static class MyMain { public static void Main() { Console.Write("Enter the value of a:"); int a = Convert.ToInt16(Console.ReadLine()); Console.Write("Enter the value of b:"); int b = Convert.ToInt16(Console.ReadLine()); ByValueByRef obj = new ByValueByRef(a , b); Console.Write("\nInitial value's a:{0}\tb:{1}", a, b); obj.callByValue(a, b); Console.Write("\n\nAfter Call By Value a:{0}\tb:{1}", a, b); obj.callByRef(ref a, ref b); Console.Write("\n\nAfter Call By Ref a:{0}\tb:{1}", a, b); Console.ReadLine();}}}

Output: Enter the value of a:10 Enter the value of b:20 Initial value's a:10 b:20

In Call By Value method a:20 b:10 After Call By Value In Call By Ref method After Call By Ref a:10 b:20 a:20 b:10 a:20 b:10

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

A r y a n | 24

Q.14 WAP to implement all the properties and methods of String class and DateTime class. Solution
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string str1 = "one"; string str2 = "one"; string str3 = "ONE"; string str4 = "two"; string str5 = "one, too"; if (String.Compare(str1, str2) == 0) Console.WriteLine(str1 + " and " + str2 + " are equal."); else Console.WriteLine(str1 + " and " + str2 + " are not equal."); if (String.Compare(str1, str3) == 0) Console.WriteLine(str1 + " and " + str3 + " are equal."); else Console.WriteLine(str1 + " and " + str3 + " are not equal."); if (String.Compare(str1, str3, true) == 0) Console.WriteLine(str1 + " and " + str3 + " are equal ignoring case."); else Console.WriteLine(str1 + " and " + str3 + " are not equal ignoring case."); if (String.Compare(str1, str5) == 0) Console.WriteLine(str1 + " and " + str5 + " are equal."); else Console.WriteLine(str1 + " and " + str5 + " are not equal."); if (String.Compare(str1, 0, str5, 0, 3) == 0) Console.WriteLine("First part of " + str1 + " and " + str5 + " are equal."); else Console.WriteLine("First part of " + str1 + " and " + str5 + " are not equal."); int result = String.Compare(str1, str4); if (result < 0) Console.WriteLine(str1 + " is less than " + str4); else if (result > 0) Console.WriteLine(str1 + " is greater than " + str4); else Console.WriteLine(str1 + " equals " + str4);

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment
// Compare strings using StringComparison enumeration. string pswd = "we~23&blx$"; string str; Console.WriteLine("Enter password: "); str = Console.ReadLine(); if (String.Compare(pswd, str, StringComparison.InvariantCulture) == 0) Console.WriteLine("Password accepted."); else Console.WriteLine("Password invalid."); // Demonstrate Concat(). string result1 = String.Concat("This ", "is ", "a ", "test ", "of ", "the ", "String ", "class."); Console.WriteLine("result: " + result1); Console.ReadKey(); // search ing string string str6= "C# has powerful string handling."; int idx; Console.WriteLine("str: " + str6); idx = str6.IndexOf('h'); Console.WriteLine("Index of first 'h': " + idx); idx = str6.LastIndexOf('h'); Console.WriteLine("Index of last 'h': " + idx); idx = str6.IndexOf("ing"); Console.WriteLine("Index of first \"ing\": " + idx); idx = str6.LastIndexOf("ing"); Console.WriteLine("Index of last \"ing\": " + idx); char[] chrs = { 'a', 'b', 'c' }; idx = str6.IndexOfAny(chrs); Console.WriteLine("Index of first 'a', 'b', or 'c': " + idx); if (str6.StartsWith("C# has")) Console.WriteLine("str begins with \"C# has\""); if (str6.EndsWith("ling.")) Console.WriteLine("str ends with \"ling.\""); Console.ReadKey(); //Spliting string str7 = "One if by land, two if by sea."; char[] seps = { ' ', '.', ',' }; string[] parts = str7.Split(seps); Console.WriteLine("Pieces from split: "); for (int i = 0; i < parts.Length; i++) Console.WriteLine(parts[i]); string whole = String.Join(" | ", parts);

A r y a n | 25

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment
Console.WriteLine("Result of join: "); Console.WriteLine(whole); string str8 = "This test"; Console.WriteLine("Original string: " + str8); // Insert str8 = str8.Insert(5, "is a "); Console.WriteLine(str); // Replace string str8 = str8.Replace("is", "was"); Console.WriteLine(str); // Replace characters str8 = str8.Replace('a', 'X'); Console.WriteLine(str); // Remove str8 = str8.Remove(4, 5); Console.WriteLine(str8); Console.ReadKey(); string[] input = { "100 + 19", "100 / 3.3", "-3 * 9", "100 - 87" }; char[] sepss = { ' ' }; for (int i = 0; i < input.Length; i++) { // split string into parts string[] partss = input[i].Split(sepss); Console.Write("Command: "); for (int j = 0; j < partss.Length; j++) Console.Write(parts[j] + " "); Console.Write(", Result: "); double n = Double.Parse(partss[0]); double n2 = Double.Parse(partss[2]); switch (partss[1]) { case "+": Console.WriteLine(n break; case "-": Console.WriteLine(n break; case "*": Console.WriteLine(n break; case "/": Console.WriteLine(n break; } }

A r y a n | 26

+ n2); - n2);

* n2); / n2);

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment
string str9 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; Console.WriteLine("str: " + str9); Console.Write("str.Substring(15): "); string substr = str9.Substring(15); Console.WriteLine(substr); Console.Write("str.Substring(0, 15): "); substr = str9.Substring(0, 15); Console.WriteLine(substr); Console.ReadKey(); } } }

A r y a n | 27

one and one are equal. one and ONE are not equal. one and ONE are equal ignoring case. one and one, too are not equal. First part of one and one, too are equal. one is less than two Enter password: 4578 Password invalid. result: This is a test of the String class. str: C# has powerful string handling. Index of first 'h': 3 Index of last 'h': 23 Index of first "ing": 19 Index of last "ing": 28 Index of first 'a', 'b', or 'c': 4 str begins with "C# has" str ends with "ling." Pieces from split: One if by land two if by sea Result of join: One | if | by | land | | two | if | by | sea | Original string: This test 4578 4578 4578 ThwX X test Command: One if by , Result: 119 Command: One if by , Result: 30.3030303030303 Command: One if by , Result: -27 Command: One if by , Result: 13 str: ABCDEFGHIJKLMNOPQRSTUVWXYZ str.Substring(15): PQRSTUVWXYZ str.Substring(0, 15): ABCDEFGHIJKLMNO

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

A r y a n | 28

Date Time
using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { DateTime dt = DateTime.Now; // obtain current time Console.WriteLine("d format: {0:d}", dt); Console.WriteLine("D format: {0:D}", dt); Console.WriteLine("t format: {0:t}", dt); Console.WriteLine("T format: {0:T}", dt); Console.WriteLine("f format: {0:f}", dt); Console.WriteLine("F format: {0:F}", dt); Console.WriteLine("g format: {0:g}", dt); Console.WriteLine("G format: {0:G}", dt); Console.WriteLine("m format: {0:m}", dt); Console.WriteLine("M format: {0:M}", dt); Console.WriteLine("o format: {0:o}", dt); Console.WriteLine("O format: {0:O}", dt); Console.WriteLine("r format: {0:r}", dt); Console.WriteLine("R format: {0:R}", dt); Console.WriteLine("s format: {0:s}", dt); Console.WriteLine("u format: {0:u}", dt); Console.WriteLine("U format: {0:U}", dt); Console.WriteLine("y format: {0:y}", dt); Console.WriteLine("Y format: {0:Y}", dt); string t; int seconds; DateTime dt1 = DateTime.Now; seconds = dt1.Second; for(int i= 0; i<50; i++) { dt1 = DateTime.Now; // update time if seconds change if(seconds != dt1.Second) { seconds = dt1.Second; t = dt1.ToString("T"); if(dt1.Minute==0 && dt1.Second==0) t = t + "\a"; // ring bell at top of hour Console.WriteLine(t);

Enrollment no 04616204410

Aryan.baliyan@gmail.com

C# First Assignment

A r y a n | 29

} } Console.ReadKey();

DateTime dt2 = DateTime.Now; Console.WriteLine("Time is {0:hh:mm tt}", dt2); Console.WriteLine("24 hour time is {0:HH:mm}", dt2); Console.WriteLine("Date is {0:ddd MMM dd, yyyy}", dt2); Console.WriteLine("Era: {0:gg}", dt2); Console.WriteLine("Time with seconds: " + "{0:HH:mm:ss tt}", dt2); Console.WriteLine("Use m for day of month: {0:m}", dt2); Console.WriteLine("use m for minutes: {0:%m}", dt2); Console.ReadKey(); } } } Output:

d format: 10/11/2011 D format: Tuesday, October 11, 2011 t format: 10:43 PM T format: 10:43:51 PM f format: Tuesday, October 11, 2011 10:43 PM F format: Tuesday, October 11, 2011 10:43:51 PM g format: 10/11/2011 10:43 PM G format: 10/11/2011 10:43:51 PM m format: October 11 M format: October 11 o format: 2011-10-11T22:43:51.7187500+05:30 O format: 2011-10-11T22:43:51.7187500+05:30 r format: Tue, 11 Oct 2011 22:43:51 GMT R format: Tue, 11 Oct 2011 22:43:51 GMT s format: 2011-10-11T22:43:51 u format: 2011-10-11 22:43:51Z U format: Tuesday, October 11, 2011 5:13:51 PM y format: October, 2011 Y format: October, 2011 Time is 10:43 PM 24 hour time is 22:43 Date is Tue Oct 11, 2011 Era: A.D. Time with seconds: 22:43:53 PM Use m for day of month: October 11 use m for minutes: 43

Enrollment no 04616204410

Aryan.baliyan@gmail.com

You might also like